问题
I'm debugging some code for a client and found the following syntax:
switch ($i) {
case 0;
echo "i equals 0";
break;
case 1;
echo "i equals 1";
break;
case 2;
echo "i equals 2";
break;
}
The case statements end in semi-colons rather than colons. Turns out this does compile, but is it legit? I've never seen that syntax before.
回答1:
From the documentation:
It's possible to use a semicolon instead of a colon after a case like:
switch($beer) { case 'tuborg'; case 'carlsberg'; case 'heineken'; echo 'Good choice'; break; default; echo 'Please make a new selection...'; break; }
回答2:
As you can check here, it works: http://codepad.org/hOLQP98D i think it works because it falls through
回答3:
Yup, just as long as $i has a number value
来源:https://stackoverflow.com/questions/9266561/semi-colon-after-a-case-valid-in-a-php-switch-statement