So I wrote a test script to see exactly how php evaluates different
combinations. The fact that it saw null, 0 empty string and an unset
variable as equal surprised me.
The following expressions are all true in php:
nullVar == notSet
nullVar == zero
nullVar == emptyString
zero == notSet
zero == emptyString
emptyString == notSet
is_null(nullVar) == TRUE
is_null(notSet) == TRUE
isset(zero) == TRUE
isset(emptyString) == TRUE
is_null(zero) == FALSE
is_null(emptyString) == FALSE
isset(notSet) == FALSE
isset(nullVar) == FALSE
Notably the following pairs of expressions at first glance seem to
contradict:
nullVar == zero
is_null(zero) == FALSE
nullVar == emptyString
is_null(emptyString) == FALSE
emptyString == notSet
isset(emptyString) == TRUE
I'm sure this is documented. But day to day this kind of stuff will be
tricky to remember, so wondered if anyone had a good simple rule to
apply?
Thanks,
(Test code):
<?php
$nullVar=null;
$zero=0;
$emptyString="";
function boolToOp($bool)
{
return $bool ? " == " : " != ";
}
function boolToString($bool)
{
return $bool ? "TRUE" : "FALSE";
}
// Test null
echo "nullVar";
echo boolToOp($nullVar == $notSet);
echo "notSet"."<br/>";
// returns true
echo "nullVar";
echo boolToOp($nullVar == $zero);
echo "zero"."<br/>";
// returns true
echo "nullVar";
echo boolToOp($nullVar == $emptyString);
echo "emptyString"."<br/>";
// returns true
echo "zero";
echo boolToOp($zero == $notSet);
echo "notSet"."<br/>";
// returns true
echo "zero";
echo boolToOp($zero == $emptyString);
echo "emptyString"."<br/>";
// returns true
echo "emptyString";
echo boolToOp($emptyString == $notSet);
echo "notSet"."<br/>";
// returns true
echo "is_null(nullVar) == ";
echo boolToString(is_null($nullVar));
echo "<br/>";
// returns true
echo "is_null(notSet) == ";
echo boolToString(is_null($notSet));
echo "<br/>";
// returns true
echo "isset(zero) == ";
echo boolToString(isset($zero));
echo "<br/>";
// returns true
echo "isset(emptyString) == ";
echo boolToString(isset($emptyString));
echo "<br/>";
// returns true
echo "is_null(zero) == ";
echo boolToString(is_null($zero));
echo "<br/>";
// returns false
echo "is_null(emptyString) == ";
echo boolToString(is_null($emptyString));
echo "<br/>";
// returns false
echo "isset(notSet) == ";
echo boolToString(isset($notSet));
echo "<br/>";
// returns false
echo "isset(nullVar) == ";
echo boolToString(isset($nullVar));
echo "<br/>";
// returns false
?>
http://es.php.net/manual/en/types.comparisons.php
> But day to day this kind of stuff will be
> tricky to remember, so wondered if anyone had a good simple rule to
> apply?
My own personal thumb rule is this:
The == operators disregards the data type so it needs to convert one of
the operands into the other operand's type. Converting a number or
string into NULL makes little sense so it converts the NULL into the
other type. That makes it more intuitive IMHO:
NULL==0 --> NULL into number: 0 --> 0==0 --> TRUE
NULL=='' --> NULL into string: '' --> ''=='' --> TRUE
isnull() and isset() are pretty intuitive if you consider that setting a
variable to NULL is sort of unsetting it
And empty()... Well, I never use it, I can't really understand it ;-)
--
-- http://alvaro.es - �lvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programaci�n web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
<snip bunch of tests>
Did you have error reporting turned on for these tests? If you did then you
would have raised a bunch of errors.
That's exactly what I needed. Thanks!