After the little experiment with PHP type comparison operation, I tried to find out what sort of comparison test the PHP switch-case statement enforces. The following is the code:
$cmd = 0;
case 'test': echo 'Inside Test case'; break;
default : echo 'Inside default case'; break;
To my surprise, the output was ‘Inside Test case’. The switch-case statement performs loose comparison behind the scene! If the case constructs construct perform complicated operations such as database manipulation or file handling, a zero parameter to the switch will create unwanted side-effects. This can be avoided if a case for handling zero input is inserted as the first case of the switch.
case 0 : /*do nothing*/ break;
However, if the 0 parameter comes directly from user via HTTP GET or POST request, it is automatically converted to a string and will not compare equal to any other string except maybe the empty string. You don’t need to add the zero case there.