In PHP, what is the difference between is_null and ==null in PHP? What are the qualifications for both to return true?
Asked
Active
Viewed 8.4k times
58
Paul Hiemstra
- 59,984
- 12
- 142
- 149
kel_ff0080
- 759
- 2
- 6
- 11
-
3This is not a duplicate - the other question asks about "===", not "==" – derekdreery Dec 02 '15 at 13:07
6 Answers
71
is_null is the same as === null. Both return true when a variable is null (or unset).
Note that I'm using === and not ==. === compares type as well as value.
gen_Eric
- 223,194
- 41
- 299
- 337
-
If the variable isn't set, `is_null()` returns `true` but also throws a "notice: undefined variable" error. To avoid this, you can do `!isset($var)` which is equivalent, the only drawback is that you can pass only variables to `isset()`. – Gras Double Jan 13 '13 at 18:37
-
@Rocket, When you say "is the same", do you mean **identical** also in terms of the errors and warnings that they will generate? – Pacerier Mar 08 '15 at 22:13
-
-
@RocketHazmat, I mean in Zend and HHVM. Basically, what I mean is, are they treated as **synonyms** in PHP? – Pacerier Mar 10 '15 at 20:48
-
-
@RocketHazmat, Yea, we need [more than just output tests.](http://3v4l.org/sTPoO) We need authoritative reference to answer that question. – Pacerier Mar 12 '15 at 07:30
-
2@Pacerier that's something that can be easily elicited by reading the source code, if you need to know the exact implementation of `isset($undefined)` vs `is_null($undefined)`. If it quacks like a duck and walks like a duck. – Justin Mitchell Mar 30 '16 at 03:52
-
2This answer would be improved if it discussed the difference between `==` and `===`, since OP asked about `==`. See Daniel Ribeiro's answer for some test cases that help show the difference. Also see dleiftah's answer for link to charts that demonstrate the difference thoroughly. – ToolmakerSteve Aug 17 '16 at 18:18
67
So you can understand it better:
$a = null;
$b = 0;
is_null($a) // TRUE
$a == null // TRUE
$a === null // TRUE
is_null($b) // FALSE
$b == null // TRUE
$b === null // FALSE
Daniel Ribeiro
- 10,156
- 12
- 47
- 79
13
There are a couple really good charts on the php.net site that show how different values react:
keithhatfield
- 3,273
- 1
- 17
- 24
5
You can check the comparison between is_null() and null === $var
Umut KIRGÖZ
- 2,105
- 3
- 22
- 29
-
2FYI, to be clear, that is a chart comparing PERFORMANCE; I followed the link to see if there was some subtle situation where they are SEMANTICALLY different, but that is not what it is about. – ToolmakerSteve Aug 17 '16 at 18:13
5
===null is recommended by Rasmus Lerdorf, the inventor of PHP.
Rasmus says the test for null is faster than the test of isset. His recommendation is sufficient reason to look at the difference seriously. The difference would be significant if you had a small loop going through the same code thousands of times in the one Web page request.
UPD: Some speed test for is_null and strict comparison:
PHP 5.5.9
is_null - float(2.2381200790405)
=== - float(1.0024659633636)
PHP 7.0.0-dev
is_null - float(1.4121870994568)
=== - float(1.4577329158783)
Akim Kelar
- 635
- 9
- 16
-
7When you say that someone says something, link or [citation needed](http://xkcd.com/285). – Pacerier Jul 12 '15 at 16:00
-
FWIW, this is not what is being asked by OP. `isset` has a different meaning. – ToolmakerSteve Aug 17 '16 at 18:16
-
@ToolmakerSteve Theme about returned value was described in best answer (marker) very well. This is addition about difference in speed – Akim Kelar Aug 25 '16 at 15:22
-
@AkimKelar - I'm pointing out that your answer is about the difference in speed between **`isset`** and `===null` - which doesn't help in deciding whether to use `is_null` or `===null`, the topic of this question. (In case someone comes along and doesn't read carefully!) – ToolmakerSteve May 06 '19 at 21:34
-
@ToolmakerSteve isn't it obvious that any non-strict comparison methods (especially methods) won't be faster than strict, is it? If no I add some speed test to answer – Akim Kelar May 15 '19 at 14:46