lang2 lang2 - 6 months ago 12
PHP Question

php null value confusion in switch statement

I have the following php code that gives me an unexpected result:

$foo = NULL;

switch($foo)
{
case 0:
print "What?!";
}


I'd expect the result to be nothing, but it matches case 0. The php manual says that NULL is a non-value, so how can it equal 0?

Answer

The switch statement applies loose comparison which means that the following things are treated as equivalent to 0:

false
0
"0"
NULL
"any string"
""
Comments