Forum Moderators: coopster

Message Too Old, No Replies

Evaluation with == and === on NULL values

Most unexpected results

         

grandpa

1:46 pm on Feb 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm seeking enlightenment.

With the following bit of code I was always returning the value of 8 in $shipvia. Using echo, I could verify that the correct value was being assigned in the foreach loop. But when I exit the loop and verify the value is not still NULL, I would always be given the default value of 8.

Changing the evaluation from == to === cured the problem and the value was returned correctly from the foreach loop.
Does this indicate a Unicode or type casting problem, or something else?

$shipvia = NULL;

foreach ($shiptable2 as $shipkey=>$shipname)
{
if ($shipper == $shipname)
{
$shipvia = $shipkey;
break;
}
}

if ($shipvia == NULL) $shipvia = 8;

ergophobe

4:26 pm on Feb 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My first guess would be that your default shipping method is zero-indexed instead of 1-indexed. In other words, the value of $shipvia can be zero. Is that right?

In some languages, any comparison with NULL evaluates to false (even NULL == NULL), but PHP isn't one of them. In PHP it goes like this:

if (NULL == 0) -> this is true - both evaluate to 0 even though different types

if (NULL === 0) -> this is false because the types don't match.

Does that sound like it might explain things?

grandpa

5:01 pm on Feb 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



At least it's a type problem (I'm not up to Unicode today..). Yes, the default ship method is 0 indexed. If I understand, then,
if ($shipvia == NULL) would be exactly the same as if ($shipvia == 0), which was not at all what I intended. I guess now I need to search my code for every evaluation of NULL and make sure I'm getting results I expect.

It's just my opinion, based on my experience, that NULL should evaluate to nothing; not blank, not 0, but nothing - the equivalent of a progamming black hole from which no 0's or 1's will ever escape.

ergophobe

11:35 pm on Feb 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I started a comment on this and then decided not to muddy the waters, but since you raise the question...

PHP can't do as you suggest for a very simple reason. Undefined variables are all NULL until you define them, but for historical reasons, PHP needs to treat them as 0, boolean false, or an empty string depending. PHP allows things like

<?
if (!$var1)
{
$var2 .= $var3;
}

echo $var2;

If you set error reporting to suppress notices and warnings, PHP will let this pass as no variable declaration is, strictly speaking, required and it will evaluate to

if (!FALSE)
{
$var2 = "" . "";
}

echo "";

If you want PHP to treat NULL values as some other more strongly typed languages do, it would evaluate to

if (?)
{
$var2 =? .?;
}
echo?;

where? are undefined values that the processor can't figure out what to do with. Since PHP doesn't behave ths way, it can't treat NULL values as


a progamming black hole from which no 0's or 1's will ever escape.

If it did so, it would need to throw a fatal error and not just a warning when confronted with an undefined variable.