在研究使用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”将下降,穿过直到碰到一个break
锻炼。我只想知道是否有一种方法可以在不重复太多代码的情况下优雅地完成此任务。
这通常称为跌落;我认为直到现在我还没有看到它叫做“级联”。 – NullUserException 2012-04-06 15:25:04
我从来没有见过重复案例的switch语句。 – 2012-04-06 15:25:27
多个案例在PHP afaik中工作正常。 – powerbuoy 2012-04-06 15:31:45