php switch 语句 returns 无效输出

php switch statement returns invalid output

php switch 语句 returns 无效输出:

$cond = "Night";

switch($cond){
    case "Morning":
    case "Night":
        $name = "Good";

    case "Morning" :
        echo $name." Morning";
        break;

    case "Night" :
        echo $name." Night";
        break;
}

输出:Good Morning

预计Good Night

编辑(阅读回复后):

感谢您的回答。 我没有得到明确的答案。

我知道我可以使用另一个语句,例如 if else。 但从逻辑上讲,代码应该可以正常工作(我认为)。 因为在 2 first cases 之后没有 break

但是为什么执行到case "Morning"时,条件为真,而实际上却不为真?

看来这里的顺序很重要,如果你用过:

$cond = "Night";

switch($cond){
    case "Morning":
    case "Night":
        $name = "Good";

    case "Night" :
        echo $name." Night";
        break;        

    case "Morning" :
        echo $name." Morning";
        break;
}

你会得到你所期望的。但是,如您所见,无法轻易预测确切的结果,因此根据您的需要,我会以不同的方式进行,例如我会使用 2 个开关:

$cond = "Night";
$name = '';
switch($cond){
    case "Morning":
    case "Night":
        $name = "Good";
        break;
}

switch ($cond) {
    case "Morning" :
        echo $name." Morning";
        break;    
    case "Night" :
        echo $name." Night";
        break;
}

或更简单:

$cond = "Night";
$name = '';
switch($cond){
    case "Morning":
    case "Night":
        $name = "Good";
        break;
}

switch ($cond) {
    case "Morning" :
    case "Night" :
        echo $name." ".$cond;
        break;
}

现在一切都将一目了然,没有人会难以理解这里发生的事情。

因为您没有添加 break 第一种情况 - 执行继续并在

之后中断
echo $name." Morning";

但即使您为第一个案例添加 break 语句 - 您也不会到达最后一个案例:

case "Night" :
    echo $name." Night";
    break;

因为执行已经超出了 switch-块。

所以你必须用另一种逻辑编写一些其他代码。简单的一个是:

$cond = "Night";

switch($cond){
    case "Morning":
    case "Night":
        echo "Good" . $cond;
        break;
}

您可以稍作不同 - 因为总会有一个 good 您可以这样做:

$cond = "Night";
$pre='Good ';

switch($cond){
    case "Morning" : echo $pre." Morning"; break;
    case "Night" : echo $pre." Night"; break;
}

试试这个代码。

$cond = "Night";
$name = "Good ";
switch($cond){
    case "Morning":
        echo $name . $cond;
    break;
    case "Night":
        echo $name . $cond;
    break;
}

为了让事情井井有条,您可以这样做。您将不需要 switch

$cond = "Night";
$name = array (
     'Morning' => 'Good',
     'Night' => 'Good'
     );
echo $name[ $cond ]." ".$cond;

在这种情况下,您不必为新情况添加另一个 switch case,只需向 $name 数组添加一个条目

如果你不破;您的切换将继续并进入下一个案例。

    $cond = "Night";

switch($cond){
    case "Morning":
    case "Night":
        $name = "Good";
        break; // add this

    case "Morning" :
        echo $name." Morning";
        break;

    case "Night" :
        echo $name." Night";
        break;
}

其他语言也会发生这种情况,例如 java,称为 fall through

当您不指定 break 时,switch 构造总是有点风险,因为执行将继续下一个案例而不测试这些条件。

docs中所述,本人以粗体字强调:

It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees 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.

因此,需要明确的是:PHP 在 case 中开始执行语句后, 不再 验证案例条件。如果没有 break,它只会继续下一个案例的陈述而不验证案例条件。因此,它会逐一执行所有语句,直到找到 breakswitch.

的结尾

所以下面的代码:

switch (1) {
case 1:
    echo "one ";
case 2:
    echo "two ";
case "hello":
    echo "hello ";
}

将输出:

one two hello

PHP 发现第一个 case 条件为真并开始执行语句而不评估任何其他 case 条件。

这可能很counter-intuitive,也让代码的可读性变差了。最好在每个 case 的末尾放置一个 break 以避免任何误解。