Why does in_array() wrongly return true with these (large numeric) strings?

后端 未结 9 2068
夕颜
夕颜 2020-12-08 00:35

I am not getting what is wrong with this code. It\'s returning \"Found\", which it should not.

$lead = \"418176000000069007\";
$diff = array(\"41817600000006         


        
相关标签:
9条回答
  • 2020-12-08 00:53

    Note: this behavior was changed in PHP 5.4.

    By default, in_array uses loose comparison (==), which means numeric strings are converted to numbers and compared as numbers. Before PHP 5.4, if you didn't have enough precision in your platform's floating-point type, the difference was lost and you got the wrong answer.

    A solution is to turn on strict comparison (===) by passing an extra Boolean parameter to in_array:

      $lead = "418176000000069007";
      $diff = array("418176000000069003", "418176000000057001");
    
      if ( in_array($lead, $diff, true) ) 
        echo "Found";
      else
        echo "Not found";
    

    Then the strings are compared as strings with no numeric coercion. However, this means you do lose the default equivalence of strings like "01234" and "1234".

    This behavior was reported as a bug and fixed in PHP 5.4. Numeric strings are still converted to numbers when compared with ==, but only if the value of the string fits in the platform's numeric type.

    0 讨论(0)
  • 2020-12-08 00:56

    in_array should be stricted.

    $lead = "418176000000069007";
      $diff = array("418176000000069003","418176000000057001");
    
      if(in_array($lead,$diff,true)) 
        echo "Found";
      else
        echo "Not found";
    

    This problem is due to your numbers are exceeded from the defined integer limit

    Note: the maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, intval('1000000000000') will return 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807.

    0 讨论(0)
  • 2020-12-08 01:00

    If that is your problem and you really want to compare/find in array then there is a trick

    $lead = "a418176000000069007";
    $diff = array("a418176000000069003","a418176000000057001");
    
    if (in_array($lead,$diff))
        echo "Found";
    else
        echo "Not found";
    

    i.e. somehow you have to prepend a perticular character to every number. They will behave as strings in comparison and hence give correct result.

    0 讨论(0)
  • 2020-12-08 01:03

    The values exceed PHP_INT_MAX. Try doing if(in_array($lead,$diff,true)) instead.

    0 讨论(0)
  • 2020-12-08 01:05

    Try using brackets and use strict mode:

    $lead = "418176000000069007";
    $diff = array("418176000000069003","418176000000057001");
    
    if(in_array($lead, $diff, true)) {
        echo "Found";
    } else {
        echo "Not found";
    }
    
    0 讨论(0)
  • 2020-12-08 01:09

    Note: This was a bug in PHP old versions and is corrected in PHP 5.4 and newer versions.

    It is because of the limitations of the number storage in PHP

    The real problem here is because of the PHP_INT_MAX - the value exceeded in our case.

    Try to echo/print_r $lead and $diff without using the quotes. It will result

    $lead ---> 418176000000070000  
    $diff ---> Array ( [0] => 418176000000070000 [1] => 418176000000060000 )
    

    so, in this case, the in_array result is true!

    so use strict comparison in in_array() by setting third argument in in_array() as true

         if(in_array($lead,$diff,true)) //use type too
           echo "Found";
         else
           echo "Not found";
    ?>
    

    Try this. It will work.

    0 讨论(0)
提交回复
热议问题