Isaac Bosca Isaac Bosca - 2 months ago 30
PHP Question

Switch condition evaluates incorrectly on zero

I was trying a piece of code like this:

$event = 0;

switch ($event) {
case 'content':
echo "/content";
case 'start':
echo "/start";
default :
echo "not available";

This code prints
when is executed, so... 0 (zero, integer, assigned to $event variable) is being evaluated as 'content'. Why?

Jay Jay

This is what is actually happening

0 == 'content'

Php tries to case the string content to a integer. Since content doesnt equal an int, it just defaults to 0.

Resulting in:

0 == 0