Over the years, I’ve encountered a lot of strange things in PHP, but this one is off the scale:
$value = 'zero';
switch ($value) {
case 0:
echo 'value is zero';
break;
default:
echo 'value is not zero';
break;
}
echo PHP_EOL;
What do you think that the code above will print? Well…I’ll spare you the hassle and give you the answer:
% php test.php value is zero
This weird behavior originates in the fact that PHP uses the equality operator (==) instead of the identity (===) one when evaluating expressions inside a switch statement. It’s basically the same as:
if ('foo' == 0) {
echo 'bar';
}
…which also yields unexpected results. I hate this type of casting!!!
Classic example:
if ($a == false && $b == true && $a == $b) { echo "WTF???"; } else { echo "OK"; }Question: how to get “WTF?” (it’s possible!)
Nice one
I have to admit, it wasn’t all that obvious. It took several tries to get it right, but here it is:
may be PHP has not TYPE NaN.
In PHP 0 (numeric) is equal to any string!
OMG!!Z! LOL!!1
PHP has some kind of NAN, it’s not a data type though, just a constant of type float: