[英]PHP switch case default
Will the default of a switch statement get evaluated if there's a matching case before it?如果前面有匹配的情况,是否会评估 switch 语句的默认值?
ex:前任:
switch ($x) {
case ($x > 5): print "foo";
case ($x%2 == 0): print "bar";
default: print "nope";
}
so for x = 50, you'd see foo
and bar
, or foo
and bar
and nope
?所以对于 x = 50,你会看到
foo
和bar
,或者foo
和bar
和nope
?
Yes, if there is no "break", then all actions following the first case
matched will be executed.是的,如果没有“break”,那么将执行第一个匹配的
case
之后的所有操作。 The control flow will "falls through" all subsequent case
, and execute all of the actions under each subsequent case
, until a break;
控制流将“遍历”所有后续
case
,并执行每个后续case
下的所有操作,直到break;
statement is encountered, or until the end of the switch
statement is reached.遇到语句,或者直到到达
switch
语句的末尾。
In your example, if $x has a value of 50, you would actually see "nope"
.在您的示例中,如果 $x 的值为 50,您实际上会看到
"nope"
。
Note that switch
is actually performing a simple equality test, between the expression following the switch
keyword, and each expression following the case
keyword.请注意,
switch
实际上是在switch
关键字后面的表达式和case
关键字后面的每个表达式之间执行简单的相等性测试。
Your example is unusual, in that it will only display "foo"
when $x has a value of 0. (Actually, when $x has a value of 0, what you would see would be "foo bar nope".)您的示例很不寻常,因为它只会在 $x 的值为 0 时显示
"foo"
。(实际上,当 $x 的值为 0 时,您会看到“foo bar nope”。)
The expression following the case
keyword is evaluated, which in your case, example return a 0 (if the conditional test is false) or a 1 (if the conditional test is true).评估
case
关键字后面的表达式,在您的情况下,示例返回 0(如果条件测试为假)或 1(如果条件测试为真)。 And it's that value (0 or 1) that switch
will compare to $x
. switch
将与$x
进行比较的是该值(0 或 1)。
Will the default of a switch statement get evaluated if there's a matching case before it?
如果前面有匹配的情况,是否会评估 switch 语句的默认值?
In most case
s it shouldn't, because you would often have break
s in there.在大多数
case
s 它不应该,因为你经常会在那里有break
s。 However in your case it would also go to the default
.但是,在您的情况下,它也会转到
default
。
Also please try to prevent to do those single line stuff (for readability):另外请尽量避免做那些单行的事情(为了可读性):
$x = 10;
switch (true) {
case ($x > 5):
print "foo";
case ($x%2 == 0):
print "bar";
default:
print "nope";
}
Will print foobarnope
.将打印
foobarnope
。 So to answer your question: yep:-)所以回答你的问题:是的:-)
It would have been easier to try it yourself.自己尝试会更容易。
But anyway, if you don't use break in a case, all the the cases following it will be executed (including the default).但是不管怎样,如果你不在一个case中使用break,它后面的所有case都会被执行(包括默认的)。
That's not how switches work.这不是开关的工作方式。
According to the manual :根据手册:
The switch statement is similar to a series of IF statements on the same expression.
switch 语句类似于同一表达式上的一系列 IF 语句。 In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to.
在许多情况下,您可能希望将同一个变量(或表达式)与许多不同的值进行比较,并根据它等于哪个值来执行不同的代码段。 This is exactly what the switch statement is for.
这正是 switch 语句的用途。
An evaluation like像这样的评价
case ($x > 5):
simply equates to简单地等同于
case true:
or要么
case false:
depending on the value of $x
because ($x > 5)
is an EVALUATION , not a VALUE .取决于
$x
的值,因为($x > 5)
是EVALUATION ,而不是VALUE 。 Switches compare the value of the parameter to see if it equates to any of the case
s.开关比较参数的值以查看它是否等于任何
case
。
switch($x) {
case ($x > 5): // $x == ($x > 5)
echo "foo";
break;
case ($x <= 5): // $x == ($x <= 5)
echo "bar"
break;
default:
echo "default";
break;
}
The comparison in the above code is equivalent to上面代码中的比较等同于
if ($x == ($x > 5)) {
echo "foo";
} elseif ($x == ($x < 5)) {
echo "bar";
} else {
echo "five";
}
which (when $x == 50
) is equivalent to其中(当
$x == 50
时)相当于
if ($x == true) {
echo "foo";
} elseif ($x == false) {
echo "bar";
} else {
echo "five";
}
which is equivalent to这相当于
if (true) { // $x == true is the same as saying "$x is truthy"
echo "foo";
} elseif (false) {
echo "bar";
} else {
echo "five";
}
IF , however, you used your switch statement as it is intended to be used (to compare the parameter to concrete values): IF但是,您使用了预期使用的 switch 语句(将参数与具体值进行比较):
switch ($x) {
case 50:
echo "foo ";
case 30:
echo "bar ";
default:
echo "foo-bar";
}
and $x == 50
, your output would be和
$x == 50
,你的输出将是
foo bar foo-bar
due to the fact that you have no break
in your cases.因为你的案件没有
break
。
Had you added the break
keyword to the end of each case, you would only execute the code for that specific case.如果您将
break
关键字添加到每个案例的末尾,您将只执行该特定案例的代码。
switch ($x) {
case 50:
echo "foo ";
break;
case 30:
echo "bar ";
break;
default:
echo "foo-bar";
break;
}
Output:输出:
foo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.