Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Null vs Zero vs Empty String vs Not Set

1 view
Skip to first unread message

Mark Smith

unread,
Dec 15, 2009, 5:32:12 AM12/15/09
to
I was having some problems yesterday checking data when the data was
zero.

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

?>

Message has been deleted

"Álvaro G. Vicario"

unread,
Dec 15, 2009, 5:44:38 AM12/15/09
to
El 15/12/2009 11:32, Mark Smith escribi�:

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
--

rf

unread,
Dec 15, 2009, 5:42:53 AM12/15/09
to

"Mark Smith" <marksm...@jungle-monkey.com> wrote in message
news:4156d78f-619c-4f2a...@e27g2000yqd.googlegroups.com...

>I was having some problems yesterday checking data when the data was
> zero.
>
> 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:

<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.


Mark Smith

unread,
Dec 15, 2009, 8:47:27 AM12/15/09
to
On Dec 15, 10:42 am, Robert Tomsick <robert-
REMOVE_THIS_INCLUDING_DASH...@tomsick.net> wrote:

> On Tue, 15 Dec 2009 02:32:12 -0800, Mark Smith wrote:
> > I was having some problems yesterday checking data when the data was
> > zero.
>
> > 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:
>
> > [ -- snipped -- ]

> > 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?
>
> Basically, if you want to know if two things are *really* equal, you use
> ===.
>

That's exactly what I needed. Thanks!

WaffleSouffle

unread,
Dec 21, 2009, 7:03:53 AM12/21/09
to
> REMOVE_THIS_INCLUDING_DASH...@tomsick.net> wrote:
> > On Tue, 15 Dec 2009 02:32:12 -0800, Mark Smith wrote:
> > > I was having some problems yesterday checking data when the data was
> > > zero.
My personal favourite is:
<?php echo var_export('strings equate to 0' == 0, true); ?>
true
0 new messages