【问题标题】:Strange issue comparing numbers inside switch比较开关内的数字的奇怪问题
【发布时间】:2015-07-10 06:46:16
【问题描述】:

我正在尝试像这样使用 switch case 从类别 id 生成 css 类名。

我在 switch case 中有多个条件,但我们只会将这一个视为它创建奇怪的输出。

示例代码:

<?php
$value = '907';//base value

$value_array =  str_split($value);//create array of string, if its int.

var_dump($value_array);//debug whats in array

switch($value_array[0]){

case 9:

$final = 'i came inside 9';

if($value_array[1].$value_array[2] == 07){
//check whther last 2 digits are 07
    $final = 'i came inside 907';
}else if($value_array[1].$value_array[2] == 09){
//chcek whether last 2 digits are 09
    $final = 'i came inside 909';
}
break;
}

echo $final;

上面的代码输出为[$value is 907]:

array(3) {
  [0]=>
  string(1) "9"
  [1]=>
  string(1) "0"
  [2]=>
  string(1) "7"
}
i came inside 907

这是正确的行为。但如果我将基值从 907 更改为 909,则输出为 [$value is 909]

array(3) {
  [0]=>
  string(1) "9"
  [1]=>
  string(1) "0"
  [2]=>
  string(1) "9"
}
i came inside 9

输出应该是i came inside 909

  • 这是为什么呢?

  • 为什么它适用于907 而不适用于909,即使两者具有相同的数据类型?

  • 我知道它们是字符串,我应该将字符串与字符串进行比较,但为什么它适用于一个示例而不适用于另一个示例?

【问题讨论】:

    标签: php if-statement switch-statement


    【解决方案1】:

    0709octal numbers,其中09 是一个无效的八进制数,所以它最终会为0。这就是为什么你的代码不能按你想要的那样工作。

    要解决它,只需将其放在引号中,例如

    if($value_array[1].$value_array[2] === "07"){
    //check whther last 2 digits are 07
        $final = 'i came inside 907';
    }else if($value_array[1].$value_array[2] === "09"){
    //chcek whether last 2 digits are 09
        $final = 'i came inside 909';
    }
    

    【讨论】:

      【解决方案2】:

      您将数组值与格式化为八进制数的整数进行比较(请参阅http://php.net/manual/de/language.types.integer.php)。

      07 是一个有效的八进制数,代表值7,您的比较有效。

      另一方面,09 是无效的八进制数。因此比较不起作用。

      为了解决您的问题,您需要将 ' 放在值周围,以便将它们解释为字符串。

      if($value_array[1].$value_array[2] == '07'){
      //check whther last 2 digits are 07
          $final = 'i came inside 907';
      }else if($value_array[1].$value_array[2] == '09'){
      //chcek whether last 2 digits are 09
          $final = 'i came inside 909';
      }
      

      【讨论】:

        【解决方案3】:

        当你使用07,PHP interprets it as an octal number。它知道09 不是八进制,因为9 在八进制系统中无效。

        试试79,或者'07''09'

        <?php
        $value = '907'; //base value
        
        $value_array =  str_split($value); //create array of string, if its int.
        
        var_dump($value_array); //debug whats in array
        
        switch ($value_array[0])
        {
            case 9:
                $final = 'i came inside 9';
        
                if ($value_array[1].$value_array[2] == '07')
                {
                    //check whther last 2 digits are 07
                    $final = 'i came inside 907';
                }
                elseif($value_array[1].$value_array[2] == '09')
                {
                    //chcek whether last 2 digits are 09
                    $final = 'i came inside 909';
                }
        
                break;
        }
        
        echo $final;
        

        【讨论】:

          【解决方案4】:

          因为在 php 中 09 会将其视为八进制数并将其转换为 0 而对于 07 它始终为 07

          当您尝试 echo 09 时,它会输出您 007 它的 07

          所以不要松散地比较==,你需要使用严格的比较===,即

          if($value_array[1].$value_array[2] === "07"){
          //check whther last 2 digits are 07
              $final = 'i came inside 907';
          }else if($value_array[1].$value_array[2] === "09"){
          //chcek whether last 2 digits are 09
              $final = 'i came inside 909';
          }
          

          【讨论】:

            猜你喜欢
            • 2019-10-10
            • 1970-01-01
            • 2014-03-03
            • 1970-01-01
            • 2010-12-09
            • 1970-01-01
            • 2011-09-18
            • 1970-01-01
            相关资源
            最近更新 更多