You hint at a deep question: when should an expression be true?
Below, I will explain why what you are doing isn't working and how to fix it.
In many languages null
, 0
, and the empty string [""
] all evaluate to false, this can make if
statements quite succinct and intuitive, but null
, 0
, and ""
are also all of different types. How should they be compared?
This page tells us that if we have two variables being compared, then the variables are converted as follows [exiting the table at the first match]
Type of First Type of Second Then
null/string string Convert NULL to "", numerical/lexical comparison
bool/null anything Convert to bool, FALSE < TRUE
So you are comparing a null versus a number. Therefore, both the null and the number are converted to boolean. This page tells
us that in such a conversion both null
and 0
are considered FALSE
.
Your expression now reads, false==false
, which, of course, is true.
But not what you want.
This page provides a list of PHP's comparison operators.
Example Name Result
$a == $b Equal TRUE if $a equals $b after type juggling.
$a === $b Identical TRUE if $a equals $b, AND they are of the same type.
$a != $b Not equal TRUE if $a not equals $b after type juggling.
$a $b Not equal TRUE if $a not equals $b after type juggling.
$a !== $b Not identical TRUE if $a not equals $b, or they are not of the same type.
$a < $b Less than TRUE if $a is strictly less than $b.
$a > $b Greater than TRUE if $a is strictly greater than $b.
$a = $b Greater than/equal TRUE if $a is greater than or equal to $b.
The first comparator is the comparison you are using now. Note that it performs the conversions I mentioned earlier.
Using the
second comparator will fix your problem. Since a null and a number are not of the same type, the ===
comparison will return false, rather than performing type conversion as the ==
operator would.
Hope this helps.
[PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8]
is_null — Finds whether a variable is null
Description
is_null[mixed $value
]: bool
Parameters
value
The variable being evaluated.
Return Values
Returns true
if value
is null, false
otherwise.
Examples
Example #1 is_null[] example
Notice: Undefined variable: inexistent in ... bool[true] bool[true]
See Also
- The
null
type - isset[] - Determine if a variable is declared and is different than null
- is_bool[] - Finds out whether a variable is a boolean
- is_numeric[] - Finds whether a variable is a number or a numeric string
- is_float[] - Finds whether the type of a variable is float
- is_int[] - Find whether the type of a variable is integer
- is_string[] - Find whether the type of a variable is string
- is_object[] - Finds whether a variable is an object
- is_array[] - Finds whether a variable is an array
Malfist ¶
14 years ago
Micro optimization isn't worth it.
You had to do it ten million times to notice a difference, a little more than 2 seconds
$a===NULL; Took: 1.2424390316s
is_null[$a]; Took: 3.70693397522s
difference = 2.46449494362
difference/10,000,000 = 0.000000246449494362
The execution time difference between ===NULL and is_null is less than 250 nanoseconds. Go optimize something that matters.
george at fauxpanels dot com ¶
13 years ago
See how php parses different values. $var is the variable.
$var = NULL "" 0 "0" 1
strlen[$var] = 0 0 1 1 1
is_null[$var] = TRUE FALSE FALSE FALSE FALSE
$var == "" = TRUE TRUE TRUE FALSE FALSE
!$var = TRUE TRUE TRUE TRUE FALSE
!is_null[$var] = FALSE TRUE TRUE TRUE TRUE
$var != "" = FALSE FALSE FALSE TRUE TRUE
$var = FALSE FALSE FALSE FALSE TRUE
Peace!
contact dot 01834e2c at renegade334 dot me dot uk ¶
7 years ago
In PHP 7 [phpng], is_null is actually marginally faster than ===, although the performance difference between the two is far smaller.
PHP 5.5.9
is_null - float[2.2381200790405]
=== - float[1.0024659633636]
=== faster by ~100ns per call
PHP 7.0.0-dev [built: May 19 2015 10:16:06]
is_null - float[1.4121870994568]
=== - float[1.4577329158783]
is_null faster by ~5ns per call
ahamilton9 ¶
4 months ago
A quick test in 2022 on PHP 8.1 confirms there is still no need to micro-optimize NULL checks: