{"id":1707,"date":"2016-11-04T18:56:05","date_gmt":"2016-11-04T18:56:05","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=1707"},"modified":"2025-08-03T13:58:54","modified_gmt":"2025-08-03T12:58:54","slug":"converting-values-in-php","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/memory\/converting-values-in-php","title":{"rendered":"Converting Values in PHP"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Convert Boolean to String<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  $BooleanValue = $MyBoolVariable ? 1 : 0;\n  \/\/or\n  $BooleanString = $MyBoolVariable ? 'true' : 'false';<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Convert Boolean Text To Boolean<\/h4>\n\n\n\n<p>\nStrings always evaluate to boolean true unless they have a value that&#8217;s considered &#8220;empty&#8221; by PHP. &nbsp;FILTER_VALIDATE_BOOLEAN provides a solution though:\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t$UploadsEnabled = \"false\";\n\t$UploadsEnabled = filter_var($UploadsEnabled, FILTER_VALIDATE_BOOLEAN);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Convert Bytes To UInt32<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\t$handle = fopen(\"myfile.bin\", \"rb\");\n\t$Contents = fread($handle, 20); \n\t$FileByteArray = unpack(\"C*\",$Contents); \t\t\t\t\/\/&lt;&lt;&lt;Warning the outputted array starts from &#91;1] not &#91;0] !!!!!! Bloody PHP...\n\t$ByteCount = 1;\t\t\t\/\/&lt;Yes 1 not 0\n\t\/\/Get uint32_t value from block of 4 bytes\n\t$SerialNumber = $FileByteArray&#91;$ByteCount++] &lt;&lt; 24;\n\t$SerialNumber += $FileByteArray&#91;$ByteCount++] &lt;&lt; 16;\n\t$SerialNumber += $FileByteArray&#91;$ByteCount++] &lt;&lt; 8;\n\t$SerialNumber += $FileByteArray&#91;$ByteCount++];<\/code><\/pre>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<p>\n&nbsp;\n<\/p>\n\n\n\n<p>\n&nbsp;\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Convert Boolean to String Convert Boolean Text To Boolean Strings always evaluate to boolean true unless they have a value that&#8217;s considered &#8220;empty&#8221; by PHP. &nbsp;FILTER_VALIDATE_BOOLEAN provides a solution though: Convert Bytes To UInt32 \u00a0 &nbsp; &nbsp;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-1707","post","type-post","status-publish","format-standard","hentry","category-memory"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=1707"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1707\/revisions"}],"predecessor-version":[{"id":5081,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1707\/revisions\/5081"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=1707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=1707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=1707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}