Skip to content

Instantly share code, notes, and snippets.

@gjerokrsteski
Created March 11, 2015 16:09
Show Gist options
  • Save gjerokrsteski/9bdeb4af3874742ec944 to your computer and use it in GitHub Desktop.
Save gjerokrsteski/9bdeb4af3874742ec944 to your computer and use it in GitHub Desktop.
is_null($x) vs $x === null in PHP
<?php
error_reporting(E_USER_NOTICE);
//checking with ===
$a = array();
$time = microtime(true);
for($i=0;$i<10000;$i++) {
if($a[$i] === null) {
//do nothing
}
}
echo 'Testing with === ', microtime(true) - $time, "\n";
//checking with is_null()
$time = microtime(true);
for($i=0;$i<10000;$i++) {
if(is_null($a[$i])) {
//do nothing
}
}
echo 'Testing with is_null() ', microtime(true) - $time, "\n";
//checking with negation
$time = microtime(true);
for($i=0;$i<10000;$i++) {
if(!$a[$i]) {
//do nothing
}
}
echo 'Testing with negation ', microtime(true) - $time, "\n";
//checking with instanceof
$time = microtime(true);
for($i=0;$i<10000;$i++) {
if($a[$i] instanceof stdClass) {
//do nothing
}
}
echo 'Testing with instanceof ', microtime(true) - $time, "\n";
@gjerokrsteski
Copy link
Author

hmmm... ???

Testing with === 0.0075018405914307
Testing with is_null() 0.12902498245239
Testing with negation 0.0070021152496338
Testing with instanceof 0.0075008869171143

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment