{"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 id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ straight processing, variables<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">for<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">=<\/span><span style=\"color: #cc66cc\">0<\/span><span style=\"color: #339933\">;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">&lt;<\/span><span style=\"color: #000088\">$loop<\/span><span style=\"color: #339933\">;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">++<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$test<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">sin<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$val0<\/span> <span style=\"color: #339933\">*<\/span> <span style=\"color: #000088\">$val1<\/span> <span style=\"color: #339933\">+<\/span> <span style=\"color: #000088\">$val2<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">\/<\/span><span style=\"color: #000088\">$val6<\/span> <span style=\"color: #339933\">*<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">sqrt<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #990000\">log<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$val3<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">*<\/span> <span style=\"color: #990000\">abs<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$val4<\/span> <span style=\"color: #339933\">-<\/span> <span style=\"color: #cc66cc\">1<\/span><span style=\"color: #339933\">\/<\/span><span style=\"color: #000088\">$val5<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">\/<\/span> <span style=\"color: #000088\">$val7<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">*<\/span> <span style=\"color: #000088\">$val8<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ straight processing, arrays<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">for<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">=<\/span><span style=\"color: #cc66cc\">0<\/span><span style=\"color: #339933\">;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">&lt;<\/span><span style=\"color: #000088\">$loop<\/span><span style=\"color: #339933\">;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">++<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$test<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">sin<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$arrayval<\/span><span style=\"color: #339933\">&amp;<\/span><span style=\"color: #666666;font-style: italic\">#91;0&amp;#93; * $arrayval&amp;#91;1&amp;#93; + $arrayval&amp;#91;2&amp;#93;))\/$arrayval&amp;#91;6&amp;#93; * (sqrt( log($arrayval&amp;#91;3&amp;#93;) * abs($arrayval&amp;#91;4&amp;#93; - 1\/$arrayval&amp;#91;5&amp;#93;)) \/ $arrayval&amp;#91;7&amp;#93;)) * $arrayval&amp;#91;8&amp;#93;;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ straight processing, constants<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">for<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">=<\/span><span style=\"color: #cc66cc\">0<\/span><span style=\"color: #339933\">;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">&lt;<\/span><span style=\"color: #000088\">$loop<\/span><span style=\"color: #339933\">;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">++<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$test<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">sin<\/span><span style=\"color: #009900\">&#040;<\/span>VAL0 <span style=\"color: #339933\">*<\/span> VAL1 <span style=\"color: #339933\">+<\/span> VAL2<span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">\/<\/span>VAL6 <span style=\"color: #339933\">*<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">sqrt<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #990000\">log<\/span><span style=\"color: #009900\">&#040;<\/span>VAL3<span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">*<\/span> <span style=\"color: #990000\">abs<\/span><span style=\"color: #009900\">&#040;<\/span>VAL4 <span style=\"color: #339933\">-<\/span> <span style=\"color: #cc66cc\">1<\/span><span style=\"color: #339933\">\/<\/span>VAL5<span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">\/<\/span> VAL7<span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">*<\/span> VAL8<span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ function call, variables<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> testvar<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$loop<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000000;font-weight: bold\">global<\/span> <span style=\"color: #000088\">$val0<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$val1<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$val2<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$val3<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$val4<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$val5<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$val6<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$val7<\/span><span style=\"color: #339933\">,<\/span><span style=\"color: #000088\">$val8<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">for<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">=<\/span><span style=\"color: #cc66cc\">0<\/span><span style=\"color: #339933\">;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">&lt;<\/span><span style=\"color: #000088\">$loop<\/span><span style=\"color: #339933\">;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">++<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #000088\">$test<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">sin<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$val0<\/span> <span style=\"color: #339933\">*<\/span> <span style=\"color: #000088\">$val1<\/span> <span style=\"color: #339933\">+<\/span> <span style=\"color: #000088\">$val2<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">\/<\/span><span style=\"color: #000088\">$val6<\/span> <span style=\"color: #339933\">*<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">sqrt<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #990000\">log<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$val3<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">*<\/span> <span style=\"color: #990000\">abs<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$val4<\/span> <span style=\"color: #339933\">-<\/span> <span style=\"color: #cc66cc\">1<\/span><span style=\"color: #339933\">\/<\/span><span style=\"color: #000088\">$val5<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">\/<\/span> <span style=\"color: #000088\">$val7<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">*<\/span> <span style=\"color: #000088\">$val8<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ function call, array<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> testarr<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$loop<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000000;font-weight: bold\">global<\/span> <span style=\"color: #000088\">$arrayval<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">for<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">=<\/span><span style=\"color: #cc66cc\">0<\/span><span style=\"color: #339933\">;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">&lt;<\/span><span style=\"color: #000088\">$loop<\/span><span style=\"color: #339933\">;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">++<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #000088\">$test<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">sin<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$arrayval<\/span><span style=\"color: #339933\">&amp;<\/span><span style=\"color: #666666;font-style: italic\">#91;0&amp;#93; * $arrayval&amp;#91;1&amp;#93; + $arrayval&amp;#91;2&amp;#93;))\/$arrayval&amp;#91;6&amp;#93; * (sqrt( log($arrayval&amp;#91;3&amp;#93;) * abs($arrayval&amp;#91;4&amp;#93; - 1\/$arrayval&amp;#91;5&amp;#93;)) \/ $arrayval&amp;#91;7&amp;#93;)) * $arrayval&amp;#91;8&amp;#93;;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ function call, constants<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> testconst<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$loop<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">for<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">=<\/span><span style=\"color: #cc66cc\">0<\/span><span style=\"color: #339933\">;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">&lt;<\/span><span style=\"color: #000088\">$loop<\/span><span style=\"color: #339933\">;<\/span><span style=\"color: #000088\">$i<\/span><span style=\"color: #339933\">++<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #000088\">$sum<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">sin<\/span><span style=\"color: #009900\">&#040;<\/span>VAL0 <span style=\"color: #339933\">*<\/span> VAL1 <span style=\"color: #339933\">+<\/span> VAL2<span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">\/<\/span>VAL6 <span style=\"color: #339933\">*<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">sqrt<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #990000\">log<\/span><span style=\"color: #009900\">&#040;<\/span>VAL3<span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">*<\/span> <span style=\"color: #990000\">abs<\/span><span style=\"color: #009900\">&#040;<\/span>VAL4 <span style=\"color: #339933\">-<\/span> <span style=\"color: #cc66cc\">1<\/span><span style=\"color: #339933\">\/<\/span>VAL5<span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">\/<\/span> VAL7<span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">*<\/span> VAL8<span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">&amp;<\/span><span style=\"color: #666666;font-style: italic\">#91;\/php&amp;#93;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#040;<\/span>Download the full <span style=\"color: #339933\">&lt;<\/span>a href<span style=\"color: #339933\">=<\/span><span style=\"color: #0000ff\">&quot;\/download\/benchmarking-constants-variables.php&quot;<\/span><span style=\"color: #339933\">&gt;<\/span>benchmark script<span style=\"color: #339933\">&lt;\/<\/span>a<span style=\"color: #339933\">&gt;<\/span><span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">&lt;<\/span>h2<span style=\"color: #339933\">&gt;<\/span>Results<span style=\"color: #339933\">&lt;\/<\/span>h2<span style=\"color: #339933\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">&lt;<\/span>table cellpadding<span style=\"color: #339933\">=<\/span><span style=\"color: #0000ff\">&quot;5&quot;<\/span> border<span style=\"color: #339933\">=<\/span><span style=\"color: #0000ff\">&quot;1&quot;<\/span><span style=\"color: #339933\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">&lt;<\/span>tr<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;&amp;<\/span>nbsp<span style=\"color: #339933\">;&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span>PHP 4<span style=\"color: #339933\">.<\/span>4<span style=\"color: #339933\">.<\/span>2<span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span>PHP 5<span style=\"color: #339933\">.<\/span>1<span style=\"color: #339933\">.<\/span>2<span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;\/<\/span>tr<span style=\"color: #339933\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">&lt;<\/span>tr<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span>straight<span style=\"color: #339933\">,<\/span> variables<span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color:#800080\">0.0382<\/span><span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color:#800080\">0.0269<\/span><span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;\/<\/span>tr<span style=\"color: #339933\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">&lt;<\/span>tr<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span>straight<span style=\"color: #339933\">,<\/span> array<span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color:#800080\">0.0510<\/span><span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color:#800080\">0.0363<\/span><span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;\/<\/span>tr<span style=\"color: #339933\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">&lt;<\/span>tr<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span>straight<span style=\"color: #339933\">,<\/span> constants<span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color:#800080\">0.0552<\/span><span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color:#800080\">0.03848<\/span><span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;\/<\/span>tr<span style=\"color: #339933\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">&lt;<\/span>tr<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color: #000000;font-weight: bold\">function<\/span> call<span style=\"color: #339933\">,<\/span> variables<span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color:#800080\">0.0382<\/span><span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color:#800080\">0.0276<\/span><span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;\/<\/span>tr<span style=\"color: #339933\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">&lt;<\/span>tr<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color: #000000;font-weight: bold\">function<\/span> call<span style=\"color: #339933\">,<\/span> array<span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color:#800080\">0.0462<\/span><span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color:#800080\">0.0331<\/span><span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;\/<\/span>tr<span style=\"color: #339933\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">&lt;<\/span>tr<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color: #000000;font-weight: bold\">function<\/span> call<span style=\"color: #339933\">,<\/span> constants<span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color:#800080\">0.0470<\/span><span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;<\/span>td<span style=\"color: #339933\">&gt;<\/span><span style=\"color:#800080\">0.0388<\/span><span style=\"color: #339933\">&lt;\/<\/span>td<span style=\"color: #339933\">&gt;&lt;\/<\/span>tr<span style=\"color: #339933\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">&lt;\/<\/span>table<span style=\"color: #339933\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">Hey<span style=\"color: #339933\">,<\/span> where is the supposedly speed advantage of constants ? Not only using variables is faster<span style=\"color: #339933\">,<\/span> but even arrays which I thought would perform worse consistently outperform constants<span style=\"color: #339933\">.<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">&lt;<\/span>h2<span style=\"color: #339933\">&gt;<\/span>Conclusions<span style=\"color: #339933\">&lt;\/<\/span>h2<span style=\"color: #339933\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">I was not able to compare memory usage across these tests<span style=\"color: #339933\">,<\/span> but regarding speed I<span style=\"color: #0000ff\">'m certainly disappointed to see how constants are doing. Of course, speed loss is probably negligible in almost if not all situations, but still.<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">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'<\/span>s safer to <span style=\"color: #000000;font-weight: bold\">use<\/span> a <span style=\"color: #990000\">constant<\/span> that cannot be redefined than a variable<span style=\"color: #339933\">.<\/span> Consider the following <span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#091;<\/span>php<span style=\"color: #009900\">&#093;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$variable<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #cc66cc\">1<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ oops !<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span>VARIABLE <span style=\"color: #339933\">=<\/span> <span style=\"color: #cc66cc\">1<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ easy to spot and won't work<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\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 id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #cc66cc\">1<\/span> <span style=\"color: #339933\">==<\/span> <span style=\"color: #000088\">$variable<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span> <span style=\"color: #666666;font-style: italic\">\/\/ do stuff }<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\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 [&hellip;]<\/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}]}}