tags:

views:

34

answers:

2
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.

+2  A: 
switch ($i) {
    case A:
    case B:
    case C:
        $letter = 'first';
        break;
    case D:
        $letter = 'second';
        break;
    default:
        $letter = 'third';
}
Otar
+4  A: 
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.

svens
+1 because you've explained why it works.
villecoder