在研究使用switch
语句的更好方法时,我发现了这个stackoverflow示例。我想做类似的事情,但有一点不同:
switch($status)
{
case "a":
case "b":
echo "start execute code for case a and b";
case "a":
echo "continue to execute code for case a only";
case "b":
echo "continue to execute code for case b only";
case "a":
case "b":
echo "complete code execution for case a and b";
break;
case "c":
echo "execute code for case c";
break;
case "d":
echo "execute code for case d";
break;
case "e":
echo "execute code for case e";
break;
case "f":
echo "execute code for case f";
break;
default:
echo "execute code for default case";
}
是的,以上显然没有按计划进行,因为案例“a”将通过直到它达到 a break
。我只想知道是否有一种方法可以优雅地做到这一点而无需重复太多代码。