开发者

Switch, same value for multiple case

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

Is there any way to shorten first three cases?

They have the same values inside.


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 break after a case, the code below the next case is executed too.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜