[php] in_array 判断问题

来源:互联网 发布:怎么在淘宝刷好评赚钱 编辑:程序博客网 时间:2024/05/21 10:54
<?php$arr = array("Linux");if (in_array(0, $arr)) {    echo "match";}?> 

执行以上代码,0和字符串是可以匹配成功的。

原因是在in_array,如果比较的类型不匹配,并且第一个参数是0,它会返回true(不正确)。

查手册:

If the third parameter strict is set to TRUE then the in_array() function will also check thetypes of theneedle in thehaystack.  

加上类型比较后返回false(正确)

查资料发现:

http://hi.baidu.com/cnkarl/item/9eb1505653a72bc59e2667dd 

摘录如下:

1.情况一

$test = 'a';

$array = array('a', 'b', 'c');

if (in_array($test, $array)) {

echo 'in array';

} else {

    echo 'no';

}

//output: in array

 

2.情况二

$test = 'other value';

$array = array('a', 'b', 'c');

if (in_array($test, $array)) {

echo 'in array';

} else {

    echo 'no';

}

//output: no

------------------------ 看似一切都正常的分割线 -----------------------

3.情况三

$test = 0;

$array = array('a', 'b', 'c');

if (in_array($test, $array)) {

    echo 'in array';

} else {

    echo 'no';

}

//output: in array

难以置信,这不坑爹吗,0不在列表中,但是这个函数返回true。这个函数很常用,所以当某些巧合出现后,就会导致不可预料的错误。

一开始我猜测in_array函数除了在values中寻找,还会去寻找keys,后来发现不是这样的。


4.情况四

$test = '0';

$array = array('a', 'b', 'c');

if (in_array($test, $array)) {

    echo 'in array';

} else {

    echo 'no';

}

//output: no

是不是很有意思


5.原来是因为:

http://www.php.net/manual/en/language.operators.comparison.php

var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true

php比较数字和字符串时,会尝试将字符串转换为数字再进行比较。 例子中的 'a', 'b', 'c' 转成数字都是0,所以和0相等,in_array就返回了true。


6.如何避免

PHP的类型戏法是把双刃剑,有时候很爽,有时候很贱。

所以当in_array的needle与array中的值类型是否相同还不确定时,最好设置in_array函数的第三个参数 strict = true,这样在检查的时候会检查类型,数字与字符串就不会偷偷相等,也就避免类似这种问题。

$test = 0;

$array = array('a', 'b', 'c');

if (in_array($test, $array, true)) {

    echo 'in array';

} else {

    echo 'no';

}

//output: no