in_array()不起作用 [英] in_array() does not work
本文介绍了in_array()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么会返回true.
Why does this return true.
$needle = TRUE;
$haystack = array('that', 'this');
print in_array($needle, $haystack); // 1
我知道人们可以通过 in_array()
严格参数来检查类型.我想知道为什么,具体表现出我展示的行为.
I am aware that one can pass in_array()
the strict parameter to check types. I want to know why specifically the behaviour I show is exhibited.
推荐答案
在进行松散比较(即忽略类型)时,PHP中的任何非空字符串都等于 TRUE
.您可以通过以下方法进行测试:
Any non-empty string in PHP is equal to TRUE
when loose comparison is made (i.e. type is ignored). You may test this by doing:
var_dump('this' == TRUE);
var_dump('that' == TRUE);
但是,如果进行严格比较(即考虑类型),结果会大不相同:
But the results are quite different when strict comparison is made (i.e. type is taken into consideration):
var_dump('this' === TRUE);
var_dump('that' === TRUE);
为了在功能 in_array
,则必须将可选的第三个参数设置为 TRUE
:
$needle = TRUE;
$haystack = array('that', 'this');
var_dump(in_array($needle, $haystack, TRUE));
这篇关于in_array()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文