Created
March 11, 2015 16:09
-
-
Save gjerokrsteski/9bdeb4af3874742ec944 to your computer and use it in GitHub Desktop.
is_null($x) vs $x === null in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hmmm... ???
Testing with === 0.0075018405914307
Testing with is_null() 0.12902498245239
Testing with negation 0.0070021152496338
Testing with instanceof 0.0075008869171143