Forum Moderators: coopster
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;
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?
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.
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.