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';
}
精彩评论