为什么 in_array() 使用这些(大数字)字符串错误地返回 true? [英] Why does in_array() wrongly return true with these (large numeric) strings?
问题描述
我不明白这段代码有什么问题.它正在返回找到",它不应该.
I am not getting what is wrong with this code. It's returning "Found", which it should not.
$lead = "418176000000069007";
$diff = array("418176000000069003","418176000000057001");
if (in_array($lead,$diff))
echo "Found";
else
echo "Not found";
推荐答案
注意:这是 PHP 旧版本中的一个错误,在 PHP 5.4
和更新版本中已更正.
Note: This was a bug in PHP old versions and is corrected in PHP 5.4
and newer versions.
是因为PHP
这里真正的问题是因为 PHP_INT_MAX
- 在我们的例子中超出了值.
The real problem here is because of the PHP_INT_MAX
- the value exceeded in our case.
尝试 echo
/print_r
$lead
和 $diff
不使用引号.结果是
Try to echo
/print_r
$lead
and $diff
without using the quotes. It will result
$lead ---> 418176000000070000
$diff ---> Array ( [0] => 418176000000070000 [1] => 418176000000060000 )
所以,在这种情况下,in_array
结果为真!
so, in this case, the in_array
result is true!
因此在 in_array()
中使用 strict
比较,将 in_array()
中的第三个参数设置为 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.
这篇关于为什么 in_array() 使用这些(大数字)字符串错误地返回 true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!