简体   繁体   English

请解释为什么PHP switch case在这段代码中总是执行case 0

[英]Please explain why PHP switch case is always executing case 0 in this code

Can someone please explain why the case "a" is never reached in below code and why it will always execute case 0有人可以解释为什么在下面的代码中从未达到案例“a”以及为什么它总是执行案例 0

switch ("a") {
case 0:
    echo "0";
    break;
case "a": // never reached because "a" is already matched with 0
    echo "a";
    break;
}

PHP, like JavaScript or Perl, is a loosely-typed language and will attempt to guess what you want to do. PHP 与 JavaScript 或 Perl 一样,是一种松散类型的语言,它会尝试猜测您想要做什么。 In this case, it changed your string to the closest integer it could find, which is zero.在这种情况下,它将您的字符串更改为它可以找到的最接近的整数,即零。 In other words, "a" == 0 is a true statement in PHP.换句话说, "a" == 0在 PHP 中是正确的。

More on this topic can be found in the PHP documentation .有关此主题的更多信息,请参见 PHP文档 I suggest you typecast the value in the switch statement, or replace it with an if / elseif / else construct.我建议您对switch语句中的值进行类型转换,或者将其替换为if / elseif / else构造。


As of PHP 8.0, this behaviour has changed and now the integer value will always be changed to a string before comparison between the two types.从 PHP 8.0 开始,这种行为发生了变化,现在整数值将始终在两种类型之间进行比较之前更改为字符串。 Strictly typing and comparing your variables remains the recommended practice, however.然而,严格输入和比较变量仍然是推荐的做法。

You can not used mix-cases in a switch statement as PHP will interpret the meaning of what you mean.您不能在 switch 语句中使用混合大小写,因为 PHP 会解释您的意思。

In layman's terms, it will try to find the 'value of "a"' which is not defined to the processor, and hence is 0 in this case.通俗地说,它会尝试找到未定义给处理器的“a”的值,因此在这种情况下为 0。

Same will go for the code below:下面的代码也是如此:

<?php

$x = "a";
switch($x)
{
    case "c":
        echo "c";
    break;

    case 1:
        echo "1";
    break;  

    case 0:
        echo "0";
    break;

    case "a":
        echo "a";
    break;

    case false:
        echo "false";
    break;

    default:
        echo "def";
    break;
}

?>

Documentation is available at PHP.net文档可在 PHP.net 上获得

The reason for this is because switch uses a loose comparison ==这样做的原因是因为switch使用了一个松散的比较==

That said:那说:

if ("a" == 0) // TRUE
if ("a" == true) // TRUE

Pretty much anything else will evaluate to false.几乎任何其他东西都会被评估为假。 (except "a" == "a" ) (除了"a" == "a"

So, if you have the need to compare against both strings and integers, you should just convert to string for the comparison.因此,如果您需要与字符串和整数进行比较,您应该只转换为字符串进行比较。

//$var = "a";
$var = 0;

$var = strval($var);
switch ($var) {
    case '0':
        echo "0";
        break;
    case 'a':
        echo "a";
        break;
}

The variable type used on case() should be same type used in switch(). case() 中使用的变量类型应该与 switch() 中使用的类型相同。

   <?php
    switch ("a") {
       case "0":
          echo "0";
          break;
       case "a": // never reached because "a" is already matched with 0
          echo "a";
          break;
    }

For integer type:对于整数类型:

<?php
switch (1) {
  case 0:
    echo 0;
    break;
  case 1: // never reached because "a" is already matched with 0
    echo 1;
    break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM