{"id":547,"date":"2006-06-01T21:29:29","date_gmt":"2006-06-01T19:29:29","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2006\/06\/01\/php-variables-vs-constants\/"},"modified":"2007-05-05T12:03:01","modified_gmt":"2007-05-05T10:03:01","slug":"php-variables-vs-constants","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2006\/06\/php-variables-vs-constants\/","title":{"rendered":"PHP : Variables vs Constants"},"content":{"rendered":"<p>I thought, and had been told, that using <a href=\"http:\/\/www.php.net\/constants\">constants<\/a> instead of <a href=\"http:\/\/www.php.net\/variables\">variables<\/a> in PHP was mostly a matter of speed : at run time, the engine replaces every occurrence of a constant with its hard-coded value, making code presumably faster than using variables that must be evaluated each time they are encountered. Or something like this.<\/p>\n<p>Wondering how big or meaningless the incidence would be, and being the anal retentive useless benchmarks &#038; statistics fan I am, I ran a few tests. With a surprise for me at the end. Bottom line : variables are faster ?!<br \/>\n<!--more--><\/p>\n<h2>Tests<\/h2>\n<p>To compare speed when processing some $variables, $arrays and CONSTANTS, I threw them twice into the silly formula <em>((sin(A * B + C))\/D * (sqrt( log(E) * abs(F &#8211; 1\/G)) \/ H)) * I<\/em><\/p>\n<ol>\n<li>straight processing<\/li>\n<li>function call and a global statement to access variables<\/li>\n<\/ol>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">\/\/ straight processing, variables\r\nfor($i=0;$i&lt;$loop;$i++) {\r\n\t$test = ((sin($val0 * $val1 + $val2))\/$val6 * (sqrt( log($val3) * abs($val4 - 1\/$val5)) \/ $val7)) * $val8;\r\n}\r\n\r\n\/\/ straight processing, arrays\r\nfor($i=0;$i&lt;$loop;$i++) {\r\n\t$test = ((sin($arrayval[0] * $arrayval[1] + $arrayval[2]))\/$arrayval[6] * (sqrt( log($arrayval[3]) * abs($arrayval[4] - 1\/$arrayval[5])) \/ $arrayval[7])) * $arrayval[8];\r\n}\r\n\r\n\/\/ straight processing, constants\r\nfor($i=0;$i&lt;$loop;$i++) {\r\n\t$test = ((sin(VAL0 * VAL1 + VAL2))\/VAL6 * (sqrt( log(VAL3) * abs(VAL4 - 1\/VAL5)) \/ VAL7)) * VAL8;\r\n}\r\n\r\n\/\/ function call, variables\r\nfunction testvar($loop) {\r\n\tglobal $val0,$val1,$val2,$val3,$val4,$val5,$val6,$val7,$val8;\r\n\tfor ($i=0;$i&lt;$loop;$i++) {\r\n\t\t$test = ((sin($val0 * $val1 + $val2))\/$val6 * (sqrt( log($val3) * abs($val4 - 1\/$val5)) \/ $val7)) * $val8;\r\n\t}\r\n}\r\n\r\n\/\/ function call, array\r\nfunction testarr($loop) {\r\n\tglobal $arrayval;\r\n\tfor ($i=0;$i&lt;$loop;$i++) {\r\n\t\t$test = ((sin($arrayval[0] * $arrayval[1] + $arrayval[2]))\/$arrayval[6] * (sqrt( log($arrayval[3]) * abs($arrayval[4] - 1\/$arrayval[5])) \/ $arrayval[7])) * $arrayval[8];\r\n\t}\r\n}\r\n\r\n\/\/ function call, constants\r\nfunction testconst($loop) {\r\n\tfor ($i=0;$i&lt;$loop;$i++) {\r\n\t\t$sum = ((sin(VAL0 * VAL1 + VAL2))\/VAL6 * (sqrt( log(VAL3) * abs(VAL4 - 1\/VAL5)) \/ VAL7)) * VAL8;\r\n\t}\r\n}<\/code><\/pre><\/div>\n<p>(Download the full <a href=\"\/download\/benchmarking-constants-variables.php\">benchmark script<\/a>)<\/p>\n<h2>Results<\/h2>\n<table cellpadding=\"5\" border=\"1\">\n<tr>\n<td>&nbsp;<\/td>\n<td>PHP 4.4.2<\/td>\n<td>PHP 5.1.2<\/td>\n<\/tr>\n<tr>\n<td>straight, variables<\/td>\n<td>0.0382<\/td>\n<td>0.0269<\/td>\n<\/tr>\n<tr>\n<td>straight, array<\/td>\n<td>0.0510<\/td>\n<td>0.0363<\/td>\n<\/tr>\n<tr>\n<td>straight, constants<\/td>\n<td>0.0552<\/td>\n<td>0.03848<\/td>\n<\/tr>\n<tr>\n<td>function call, variables<\/td>\n<td>0.0382<\/td>\n<td>0.0276<\/td>\n<\/tr>\n<tr>\n<td>function call, array<\/td>\n<td>0.0462<\/td>\n<td>0.0331<\/td>\n<\/tr>\n<tr>\n<td>function call, constants<\/td>\n<td>0.0470<\/td>\n<td>0.0388<\/td>\n<\/tr>\n<\/table>\n<p>Hey, where is the supposedly speed advantage of constants ? Not only using variables is faster, but even arrays which I thought would perform worse consistently outperform constants.<\/p>\n<h2>Conclusions<\/h2>\n<p>I was not able to compare memory usage across these tests, but regarding speed I&#39;m certainly disappointed to see how constants are doing. Of course, speed loss is probably negligible in almost if not all situations, but still.<\/p>\n<p>Why use constants then ? Their only advantages over variables I can think of is that constant have a global scope so you can forget a global statement, and if you mistype a test (= instead of == or ===), it&#39;s safer to use a constant that cannot be redefined than a variable. Consider the following :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-2\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">if ($variable = 1) {\r\n\/\/ oops !\r\n}\r\n\r\nif (VARIABLE = 1) {\r\n\/\/ easy to spot and won&#039;t work\r\n}<\/code><\/pre><\/div>\n<p>But this is slight advantage, and you&#39;re going safer anyway with a reverse syntax where typos generate a warning :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-3\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">if (1 == $variable) { \/\/ do stuff }<\/code><\/pre><\/div>\n<p>So, basically, I&#39;m asking : why use constants ? Are you using them ?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I thought, and had been told, that using constants instead of variables in PHP was mostly a matter of speed : at run time, the engine replaces every occurrence of a constant with its hard-coded value, making code presumably faster than using variables that must be evaluated each time they are encountered. Or something like this. Wondering how big or\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[169,2,10,74,49],"class_list":["post-547","post","type-post","status-publish","format-standard","hentry","tag-benchmark","tag-code","tag-php","tag-stats","tag-useless"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=547"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/547\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}