lang2 lang2 - 1 year ago 55
PHP Question

php null value confusion in switch statement

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

$foo = NULL;

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 Source

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

"any string"