php is null or empty?
What you're looking for is:
if($variable === NULL) {...}
Note the ===
.
When use ==
, as you did, PHP treats NULL, false, 0, the empty string, and empty arrays as equal.
As is shown in the following table, empty($foo)
is equivalent to $foo==null
and is_null($foo)
has the same function of $foo===null
. The table also shows some tricky values regarding the null
comparison. (ϕ denotes an uninitialized variables. )
empty is_null
==null ===null isset array_key_exists
ϕ | T | T | F | F
null | T | T | F | T
"" | T | F | T | T
[] | T | F | T | T
0 | T | F | T | T
false | T | F | T | T
true | F | F | T | T
1 | F | F | T | T
\0 | F | F | T | T
check ==
vs ===
'' == NULL
would return true
0 == NULL
would return true
false == null
would return true
where as
'' === NULL
would return false
0 === NULL
would return false
false === NULL
would return false