title: Converting MySQL tinyint booleans (0/1) to PHP booleans (true/false)
body: <p>I need to repeatedly convert the MySQL tinyint(1) 'boolean' datatype into a PHP boolean value, and I've been trying to test the fastest way to do this. My data mapping is as follows:</p>
  
  <ul>
  <li>NULL = FALSE</li>
  <li>0 = FALSE</li>
  <li>1 = TRUE</li>
  </ul>
  
  <p>After looking around for a few hours, I can't seem to find any performance explanations/critiques on this, so I went and tried to make one myself of the possible solutions I have found. My code is as follows:</p>
  
  <pre><code>echo 'Current PHP Version: ' . phpversion() . '&lt;br /&gt;&lt;br /&gt;';
  $start1 = 1;
  $start0 = 0;
  
  $time_start = microtime(TRUE);
  for( $i = 0 ; $i &lt; 8500000; $i++ )
  {
      $answer = !empty($start1);
      $answer = !empty($start2);  
  }
  $time_end = microtime(TRUE);
  echo 'Did NOT EMPTY in ' . ($time_end - $time_start) . " seconds&lt;br&gt;";
  
  $time_start = microtime(TRUE);
  for( $i = 0 ; $i &lt; 8500000; $i++ )
  {
      $answer = (bool)$start1;
      $answer = (bool)$start2;    
  }
  $time_end = microtime(TRUE);
  echo 'Did TYPECAST BOOL in ' . ($time_end - $time_start) . " seconds&lt;br&gt;";
  
  $time_start = microtime(TRUE);
  for( $i = 0 ; $i &lt; 8500000; $i++ )
  {
      $answer = $start1 == TRUE;
      $answer = $start2 == TRUE;  
  }
  $time_end = microtime(TRUE);
  echo 'Did EQUALS TRUE in ' . ($time_end - $time_start) . " seconds&lt;br&gt;";
  
  $time_start = microtime(TRUE);
  for( $i = 0 ; $i &lt; 8500000; $i++ )
  {
      $answer = !!$start1;
      $answer = !!$start2;    
  }
  $time_end = microtime(TRUE);
  echo 'Did NOT NOT in ' . ($time_end - $time_start) . " seconds&lt;br&gt;";
  
  $time_start = microtime(TRUE);
  for( $i = 0 ; $i &lt; 8500000; $i++ )
  {
      $answer = is_null($start1);
      $answer = is_null($start2);     
  }
  $time_end = microtime(TRUE);
  echo 'Did IS NULL in ' . ($time_end - $time_start) . " seconds&lt;br&gt;";
  </code></pre>
  
  <p>My results are as follows:</p>
  
  <pre><code>Current PHP Version: 5.4.16
  
  Did NOT EMPTY in 1.00608086586 seconds
  Did TYPECAST BOOL in 2.5599420070648 seconds
  Did EQUALS TRUE in 2.7039749622345 seconds
  Did NOT NOT in 2.7622299194336 seconds
  Did IS NULL in 4.1728219985962 seconds
  </code></pre>
  
  <p>I picked 8.5 million iterations as I generally like to bring the fastest test as close to 1 second as possible to see scaling at a glance. From this test it seems like the best choice is !empty($value) by an impressively wide margin.</p>
  
  <p>I'm posting this to share my findings and see if anyone else knows of a different way to get the same results which may be faster, or if this will radically change in future PHP versions. We're currently in a 5.4 environment, hoping to port to 5.6 in the next year, with hopes of seeing php7 eventually. </p>
  
  <p>Does one of these methods win out differently in newer versions of PHP, or is there even a completely different way to go about this that I'm missing? Thanks for your insight!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>As was pointed out, the above test is incorrect. A correct test, with an additional option, is below:</p>
  
  <pre><code>echo 'Current PHP Version: ' . phpversion() . '&lt;br /&gt;&lt;br /&gt;';
  
  $start1 = 1;
  $start0 = 0;
  
  $time_start = microtime(TRUE);
  for( $i = 0 ; $i &lt; 14000000; $i++ )
  {
      $answer = $start1 == 1;
      $answer = $start0 == 1;     
  }
  $time_end = microtime(TRUE);
  echo 'Did EQUALS 1 in ' . ($time_end - $time_start) . " seconds&lt;br&gt;";
  
  $time_start = microtime(TRUE);
  for( $i = 0 ; $i &lt; 14000000; $i++ )
  {
      $answer = (bool)$start1;
      $answer = (bool)$start0;    
  }
  $time_end = microtime(TRUE);
  echo 'Did TYPECAST BOOL in ' . ($time_end - $time_start) . " seconds&lt;br&gt;";
  
  $time_start = microtime(TRUE);
  for( $i = 0 ; $i &lt; 14000000; $i++ )
  {
      $answer = $start1 == TRUE;
      $answer = $start0 == TRUE;  
  }
  $time_end = microtime(TRUE);
  echo 'Did EQUALS TRUE in ' . ($time_end - $time_start) . " seconds&lt;br&gt;";
  
  $time_start = microtime(TRUE);
  for( $i = 0 ; $i &lt; 14000000; $i++ )
  {
      $answer = !!$start1;
      $answer = !!$start0;    
  }
  $time_end = microtime(TRUE);
  echo 'Did NOT NOT in ' . ($time_end - $time_start) . " seconds&lt;br&gt;";
  
  $time_start = microtime(TRUE);
  for( $i = 0 ; $i &lt; 14000000; $i++ )
  {
      $answer = !empty($start1);
      $answer = !empty($start0);  
  }
  $time_end = microtime(TRUE);
  echo 'Did NOT EMPTY in ' . ($time_end - $time_start) . " seconds&lt;br&gt;";
  
  $time_start = microtime(TRUE);
  for( $i = 0 ; $i &lt; 14000000; $i++ )
  {
      $answer = !is_null($start1);
      $answer = !is_null($start0);    
  }
  $time_end = microtime(TRUE);
  echo 'Did NOT IS NULL in ' . ($time_end - $time_start) . " seconds&lt;br&gt;";
  </code></pre>
  
  <p>This test gives me:</p>
  
  <pre><code>Current PHP Version: 5.4.16
  
  Did EQUALS 1 in 1.0449228286743 seconds
  Did TYPECAST BOOL in 1.0921199321747 seconds
  Did EQUALS TRUE in 1.3697588443756 seconds
  Did NOT NOT in 1.3729720115662 seconds
  Did NOT EMPTY in 1.4694240093231 seconds
  Did NOT IS NULL in 3.2058408260345 seconds
  </code></pre>
  
  <p>After seeing the possibly significant performance changes in newer version of PHP, I'm interested in how it fares moving forward, as the race is pretty close now!</p>