title: PHP string versus boolean speed test
body: <p>I'm looking at trying to optimise a particular function in a PHP application and foolishly assumed that a boolean lookup in an 'if' statement would be quicker than a string compare. But to check it I put together a short test (see below) using <a href="http://php.net/manual/en/function.microtime.php" rel="nofollow noreferrer">microtime</a>. To my surprise, the string lookup was quicker.</p>
  
  <p>Is there anything wrong with my test (I'm wired on too much coffee, so I'm suspicious of my own code)? If not, I would be interested in any comments people have around string versus boolean lookups in PHP.</p>
  
  <p>The result for the first test (boolean lookup) was 0.168 seconds.</p>
  
  <p>The result for the second test (string lookup) was 0.005 seconds.</p>
  
  <pre><code>&lt;?php
      $how_many = 1000000;
      $counter1 = 0;
      $counter2 = 0;
  
      $abc = array('boolean_lookup'=&gt;TRUE, 'string_lookup'=&gt;'something_else');
  
      $start = microtime();
      for ($i = 0; $i &lt; $how_many; $i++)
      {
          if ($abc['boolean_lookup'])
          {
              $counter1++;
          }
      }
  
      echo ($start - microtime());
  
      echo '&lt;hr&gt;';
  
      $start = microtime();
      for ($i = 0; $i &lt; $how_many; $i++)
      {
          if ($abc['string_lookup'] == 'something_else')
          {
              $counter2++;
          }
      }
  
      echo ($start - microtime());
  </code></pre>