php 开关,多个案例的相同值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3629096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:28:50  来源:igfitidea点击:

Switch, same value for multiple case

phpswitch-statement

提问by James

switch ($i) {
    case A:
        $letter = 'first';
        break;
    case B:
        $letter = 'first';
        break;
    case C:
        $letter = 'first';
        break;
    case D:
        $letter = 'second';
        break;
    default:
        $letter = 'third';
}

Is there any way to shorten first three cases?

有没有办法缩短前三个案例?

They have the same values inside.

它们内部具有相同的值。

回答by svens

switch ($i) {
    case A:
    case B:
    case C:
        $letter = 'first';
        break;
    case D:
        $letter = 'second';
        break;
    default:
        $letter = 'third';
}

Yep there is. If there's no breakafter a case, the code below the next caseis executed too.

是的。如果breaka 之后没有case,则 next 下方的代码case也会执行。

回答by Otar

switch ($i) {
    case A:
    case B:
    case C:
        $letter = 'first';
        break;
    case D:
        $letter = 'second';
        break;
    default:
        $letter = 'third';
}