php 开关功能中是否有任何“其他情况”可以使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2235877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any "Else Case" in the switch function to use?
提问by Daniel May
A switch statement consists of "cases"...
switch 语句由“cases”组成...
But is there any "else" case for all other cases?
但是对于所有其他情况是否有任何“其他”情况?
Have never found the answer to this...
一直没有找到这个问题的答案...
ex:
前任:
switch ($var){
case "x":
do stuff;
break;
case "y":
do stuff;
break;
else: // THIS IS WHAT I WOULD LIKE
do stuff;
break;
}
回答by Daniel May
default:
do stuff;
break;
Typically the defaultclause should be at the very end of your other caseclauses for general readability.
通常,为了便于阅读,该default子句应位于其他case子句的最后。
You may also want to reformat your breakstatements in your code to look like this:
您可能还想将break代码中的语句重新格式化为如下所示:
switch ($var){
case "x": // if $var == "x"
do stuff;
break;
case "y": // if $var == "y"
do stuff;
break;
default: // if $var != "x" && != "y"
do stuff;
break;
}
Extra information on the switchstatement available hereand here.
回答by Simon
As Dan said but in complete form if it helps...
正如丹所说,但如果有帮助,请以完整的形式...
switch ($var) {
case "x":
// do stuff
break;
case "y":
// do stuff
break;
default:
// do "else" stuff...
}
回答by Sebastian Mach
As the others said. Not though that default might also be at the beginning or somewhere wildly in the middle:
正如其他人所说。虽然默认值也可能在开头或中间的某个地方:
switch (foo)
{
case 0: break;
default: break;
case 1: break;
};
Surely you should not do this if not justified.
如果没有正当理由,你当然不应该这样做。

