A swi开发者_开发知识库tch statement consists of "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;
}
default:
do stuff;
break;
Typically the default
clause should be at the very end of your other case
clauses for general readability.
You may also want to reformat your break
statements in your code to look like this:
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 switch
statement available here and here.
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...
}
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.
精彩评论