Php 简明教程
PHP - Switch Statement
PHP 中的 switch 语句可以看作对相同表达式中的一系列 if…else 语句的替代。假设你需要用许多不同的值来比较一个表达式或一个变量,并且根据它等于哪个值执行不同的代码片段。在这样的情况下,你需要使用多重 if…elseif…else 构造。
The switch statement in PHP can be treated as an alternative to a series of if…else statements on the same expression. Suppose you need to compare an expression or a variable with many different values and execute a different piece of code depending on which value it equals to. In such a case, you would use multiple if…elseif…else constructs.
然而,这样的构造会使代码变得很乱,并且难以理解。为了简化这样的代码,可以在 PHP 中使用 switch case 构造,它提供了一个更简洁的替代方案来避免 if..elseif..else 代码的长块。
However, such a construct can make the code quite messy and difficult to follow. To simplify such codes, you can use the switch case construct in PHP that offers a more compact alternative to avoid long blocks of if..elseif..else codes.
下面的 PHP 脚本使用 if elseif 语句:
The following PHP script uses if elseif statements −
if ($x == 0) {
echo "x equals 0";
} elseif ($x == 1) {
echo "i equals 1";
} elseif ($x == 2) {
echo "x equals 2";
}
你可以使用 switch case 语句来获得相同的结果,如下所示:
You can get the same result by using the switch case statements as shown below −
switch ($x) {
case 0:
echo "x equals 0";
break;
case 1:
echo "x equals 1";
break;
case 2:
echo "x equals 2";
break;
}
switch 语句后跟一个表达式,该表达式与每个 case 子句中的值依次进行比较。如果发现表达式与任何 case 匹配,则执行相应的语句块。
The switch statement is followed by an expression, which is successively compared with value in each case clause. If it is found that the expression matches with any of the cases, the corresponding block of statements is executed.
-
The switch statement executes the statements inside the curly brackets line by line.
-
If and when a case statement is found whose expression evaluates to a value that matches the value of the switch expression, PHP starts to execute the statements until the end of the switch block, or the first time it encounters a break statement.
-
If you don’t write a break statement at the end of a case’s statement list, PHP will go on executing the statements of the following case.
Example
尝试移除 break,然后再运行上面的代码。如果 x 的值是 0,那么您会发现输出包括 "x equals 1" 和 "x equals 2" 这两行。
Try to run the above code by removing the breaks. If the value of x is 0, you’ll find that the output includes "x equals 1" as well as "x equals 2" lines.
<?php
$x=0;
switch ($x) {
case 0:
echo "x equals 0 \n";
case 1:
echo "x equals 1 \n";
case 2:
echo "x equals 2";
}
?>
它将生成以下 output −
It will produce the following output −
x equals 0
x equals 1
x equals 2
因此,确保以 break 语句结束每个 case 块很重要。
Thus, it is important make sure to end each case block with a break statement.
The Default Case in Switch
default case 是一个特例。这种 case 与其他 case 都不匹配。使用 default 是可选的,但如果使用,这必须是大括号内最后一个 case。
A special case is the default case. This case matches anything that wasn’t matched by the other cases. Using default is optional, but if used, it must be the last case inside the curly brackets.
您可以将多个 case 组合起来,以模拟与 or 运算符组合在一起的多个逻辑表达式。
You can club more than one cases to simulate multiple logical expressions combined with the or operator.
<?php
$x=10;
switch ($x) {
case 0:
case 1:
case 2:
echo "x between 0 and 2 \n";
break;
default:
echo "x is less than 0 or greater than 2";
}
?>
要比较的值都给在 case clause 中。值可以是一个数字、一个字符串,甚至是一个函数。不过,您不能在 case clause 中使用比较运算符 (<、>、== 或 !=) 作为值。
The values to be compared against are given in the case clause. The value can be a number, a string, or even a function. However you cannot use comparison operators (<, > == or !=) as a value in the case clause.
您可以在 case 子句中选择使用分号,而不是冒号。如果没有匹配的 case,并且也没有 default 分支,那么就不会执行任何代码,就像没有 if 语句为 true 一样。
You can choose to use semicolon instead of colon in the case clause. If no matching case found, and there is no default branch either, then no code will be executed, just as if no if statement was true.
The switch-endswitch Statement
PHP 允许使用替代语法,通过 switch-endswitch 语句来界定 switch 结构。以下版本的 switch case 是可以接受的。
PHP allows the usage of alternative syntax by delimiting the switch construct with switch-endswitch statements. The following version of switch case is acceptable.
<?php
$x=0;
switch ($x) :
case 0:
echo "x equals 0";
break;
case 1:
echo "x equals 1 \n";
break;
case 2:
echo "x equals 2 \n";
break;
default:
echo "None of the above";
endswitch
?>
Using the Break Statement in Switch…Case
显然,您不用编写 break 来终止 default case,因为它在 switch 结构中是最后一个 case。
Obviously, you needn’t write a break to terminate the default case, it being the last case in the switch construct.
Example
请看以下示例:
Take a look at the following example −
<?php
$d = date("D");
switch ($d){
case "Mon":
echo "Today is Monday";
break;
case "Tue":
echo "Today is Tuesday";
break;
case "Wed":
echo "Today is Wednesday";
break;
case "Thu":
echo "Today is Thursday";
break;
case "Fri":
echo "Today is Friday";
break;
case "Sat":
echo "Today is Saturday";
break;
case "Sun":
echo "Today is Sunday";
break;
default:
echo "Wonder which day is this ?";
}
?>
它将生成以下 output −
It will produce the following output −
Today is Monday