PHP switch case 默认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11904108/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:25:19  来源:igfitidea点击:

PHP switch case default

phpswitch-statementdefault

提问by beth

Will the default of a switch statement get evaluated if there's a matching case before it?

如果 switch 语句的默认值之前有一个匹配的 case,它会被评估吗?

ex:

前任:

switch ($x) {
  case ($x > 5): print "foo";
  case ($x%2 == 0): print "bar";
  default: print "nope";
}

so for x = 50, you'd see fooand bar, or fooand barand nope?

所以对于X = 50,你会看到foobar,或foobarnope

采纳答案by spencer7593

Yes, if there is no "break", then all actions following the first casematched will be executed. The control flow will "falls through" all subsequent case, and execute all of the actions under each subsequent case, until a break;statement is encountered, or until the end of the switchstatement is reached.

是的,如果没有“中断”,则将case执行第一个匹配项之后的所有操作。控制流将“通过”所有后续case,并执行每个后续 下的所有操作case,直到break;遇到语句,或直到switch到达语句末尾。

In your example, if $x has a value of 50, you would actually see "nope".

在您的示例中,如果 $x 的值为 50,您实际上会看到"nope".

Note that switchis actually performing a simple equality test, between the expression following the switchkeyword, and each expression following the casekeyword.

请注意,这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".)

您的示例很不寻常,因为它只会"foo"在 $x 的值为 0 时显示。(实际上,当 $x 的值为 0 时,您会看到“foo bar nope”。)

The expression following the casekeyword 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). And it's that value (0 or 1) that switchwill compare to $x.

case评估关键字后面的表达式,在您的情况下,示例返回 0(如果条件测试为假)或 1(如果条件测试为真)。它将switch$x.

回答by PeeHaa

Will the default of a switch statement get evaluated if there's a matching case before it?

如果 switch 语句的默认值之前有一个匹配的 case,它会被评估吗?

In most cases it shouldn't, because you would often have breaks in there. However in your case it would also go to the default.

在大多数cases 中它不应该,因为你经常会break在那里有s 。但是,在您的情况下,它也会转到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. So to answer your question: yep :-)

会打印foobarnope。所以回答你的问题:是的:-)

回答by Oussama

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 都将被执行(包括默认值)

回答by Matt

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. 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 语句类似于同一表达式上的一系列 IF 语句。在许多情况下,您可能希望将同一个变量(或表达式)与许多不同的值进行比较,并根据它等于的值执行不同的代码段。这正是 switch 语句的用途。

An evaluation like

评价如

case ($x > 5):

simply equates to

简单地等同于

case true:

or

或者

case false:

depending on the value of $xbecause ($x > 5)is an EVALUATION, not a VALUE. Switches compare the value of the parameter to see if it equates to any of the cases.

取决于$x因为的值($x > 5)是一个EVALUATION,而不是一个VALUE。开关比较参数的值,看它是否等于任何cases。

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

其中 (when $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 breakin your cases.

因为break在你的情况下你没有。

Had you added the breakkeyword 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