Semi-colon after a case valid in a PHP switch statement?

北战南征 提交于 2020-01-02 03:51:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!