Skip to content

Instantly share code, notes, and snippets.

@pgampe
Created November 23, 2013 13:18
Show Gist options
  • Save pgampe/7614504 to your computer and use it in GitHub Desktop.
Save pgampe/7614504 to your computer and use it in GitHub Desktop.
is_null($var) vs $var === NULL testing
<?php
$varNull = NULL;
$varNotNull = 0;
$i = 0;
$start = microtime(TRUE);
for ($i = 0; $i === 10000000; $i++) {
if (is_null($varNull)) {
}
}
$end = microtime(TRUE);
$isNullIfNull = $end - $start;
print $isNullIfNull . "\n";
$start = microtime(TRUE);
for ($i = 0; $i === 10000000; $i++) {
if (is_null($varNotNull)) {
}
}
$end = microtime(TRUE);
$isNullIfNotNull = $end - $start;
print $isNullIfNotNull . "\n";
$start = microtime(TRUE);
for ($i = 0; $i === 10000000; $i++) {
if ($varNull === NULL) {
}
}
$end = microtime(TRUE);
$nullIfNull = $end - $start;
print $nullIfNull . "\n";
$start = microtime(TRUE);
for ($i = 0; $i === 10000000; $i++) {
if ($varNotNull === NULL) {
}
}
$end = microtime(TRUE);
$nullIfNotNull = $end - $start;
print $nullIfNotNull . "\n";
print 'not null:' . ($isNullIfNotNull / $nullIfNotNull) . "\n";
print 'null:' . ($isNullIfNull / $nullIfNull) . "\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment