简体   繁体   English

带0和符号值的开关盒

[英]Switch-case with 0 and symbol values

So, there's the code of switch statement: 因此,有switch语句的代码:

$v1 = "String:value:0:1";
$v2 = explode(":",$v1);

switch($v2[0]) {
    case "String":
        switch($v2[2]) {
            case 0: print("v2[2] is first nul"); break;
            case "0": print("v2[2] is second (string) nul"); break;
            case 1: print("v2[2] is not nul"); break;
            case "a": print("v2[2] is NaN"); break;
        }
    break;
}

So, it should check if third part of array is 0. In case that it contains NaN-characters, I can't get it working: 因此,它应该检查数组的第三部分是否为0。如果它包含NaN字符,我将无法使其正常工作:

  • If $v2[2] is 0, then script puts out "v2[2] is first nul" (pure 0) 如果$ v2 [2]为0,则脚本输出“ v2 [2]为第一个nul”(纯0)
  • If $v2[2] is not 0, it works fine 如果$ v2 [2]不为0,则可以正常工作
  • If $v2[2] is NaN, then script puts out same as if value would be 0 如果$ v2 [2]为NaN,则脚本将输出与值将为0相同的内容

What is wrong? 怎么了?

Thank you. 谢谢。

[Solved]: For not that big checks using if/then/else, in my case I decided to use case expression: case ($v2[2] === 0): print("v2[2] is second (string) nul"); break; [已解决]:对于使用if / then / else的大检查,我决定使用case表达式: case ($v2[2] === 0): print("v2[2] is second (string) nul"); break; case ($v2[2] === 0): print("v2[2] is second (string) nul"); break; .

See second note on http://php.net/switch : 参见http://php.net/switch上的第二条注释:

Note that switch/case does loose comparison. 请注意,开关/外壳确实比较松散。

which links to http://php.net/manual/en/types.comparisons.php#types.comparisions-loose which in short means that switch compars like the == and not like the === operator. 链接到http://php.net/manual/zh-cn/types.comparisons.php#types.comparisions-loose ,简而言之意味着开关比较像==而不是===运算符。

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

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