What is the fastest is_null($y)
or $y == null
?
What is the fastest is_null($y)
or $y == null
?
The question you really should ask is " Which is safer to use? ". For example, the following expressions:
"oi" == null => false
"" == null => true
0 == null => true
null == null => true
is_null("oi") => false
is_null("") => false
is_null(0) => false
is_null(null) => true
"oi" === null => false
"" === null => false
0 === null => false
null === null => true
The difference between ===
and is_null
is pretty much irrelevant, unless you have about two million such expressions in a script to require meaningful optimization.
One of the comments in the PHP documentation says the following (in free translation) :
Micro optimization is not worth it.
You had to do it ten million times to notice a difference, just over 2 seconds
$a===NULL;
lasted: 1.2424390316s
is_null($a);
lasted: 3.70693397522sDifference = 2,46449494362s
Difference / 10,000,000 = 0.000000246449494362ns
The execution time between
=== NULL
andis_null
is less than 250 nanoseconds . Go optimize something worthwhile.
If you really want to optimize and have security in your code, give preference to triple-operator comparison:
$y === null
But it's not worth scraping code to optimize these expressions (unless you look for the simple ==
operator). Because the difference is so insignificant, that time would be better invested if you were cleaning / improving the code or studying.
More on logical expressions: link
More details on logical operators: link
It all depends on what you want to check, you should not just look at performance. For example:
<?php
$str1 = "";
$str2 = null;
$str3 = 0;
var_dump(is_null($str1)); // retorna false
var_dump(is_null($str2)); // retorna true
var_dump(is_null($str3)); // retorna false
var_dump($str1 == null); // retorna true
var_dump($str2 == null); // retorna true
var_dump($str3 == null); // retorna true
?>
Your question is creepy because the result is never the same for:
is_null($y) ou $y == null
To compare you should do so over:
is_null($y) ou $y === null
Here the results will be similar. It is important to mention that there are situations where the applicability of the ===
operator is impossible. In cases that need to be used in callbacks only is_null
can be used.
In a direct answer to the question regarding performance: The difference is not relevant.
More: Both are different approaches to the same problem.
Because ...
$y === null
will in the same always originate calls of C methods in either case. Opinion: I apply according to the situation, but I would always choose $ y === null because it is a base operator and for that reason nuclear ... so PHP tends to resolve with less effort certainly.
There is no difference between is_null
and === null
.
It unifies difference and that is_null
is a function and consequently,
1 ° Calling the function slows down, but this is relatively unimportant.
2 ° Because it is a function you can use it as a callback example:
array_map('is_null', $array);
In the php documentation there is a benchmarking test with 10 million iterations that ends up as follows, comparing a variable with an operator is much faster, so do not use is_null
unless you need a callback .
$ y == null .
In terms of performance, the difference is not so relevant, but if used frequently, it's really worth it, just use it.