I have found some odd behaviour while I was using the PHP function in_array()
. I have an array like this:
我在使用PHP函数in_array()时发现了一些奇怪的行为。我有一个像这样的数组:
$arr = [TRUE, "some string", "something else"];
Now if I want to check if "test"
is in the array it is clearly not, but in_array()
still returns TRUE, why is that?
现在,如果我想检查“test”是否在数组中,显然不是,但是in_array()仍然返回TRUE,为什么呢?
$result = in_array("test", $arr);
var_dump($result); //Output: bool(true)
The same thing happens when using array_search()
:
使用array_search()时会发生同样的事情:
$result = array_search("test", $arr);
var_dump($result); //Output: int(0)
I thought maybe that the value TRUE in the array was automatically causing the function to return TRUE for every result without checking the rest of the array, but I couldn't find any documentation that would suggest that very odd functionality.
我想也许数组中的值TRUE会自动导致函数为每个结果返回TRUE,而不检查数组的其余部分,但我找不到任何可能表明非常奇怪的功能的文档。
2 个解决方案
#1
8
This behaviour of the function in_array()
and array_search()
is not a bug, but instead well documented behaviour.
函数in_array()和array_search()的这种行为不是错误,而是记录良好的行为。
Both functions have a 3rd optional parameter called $strict
which by default is FALSE:
这两个函数都有一个名为$ strict的第三个可选参数,默认情况下为FALSE:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
bool in_array(混合$ needle,数组$ haystack [,bool $ strict = FALSE])
mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )
mixed array_search(mixed $ needle,array $ haystack [,bool $ strict = false])
Now what that means is that by default both functions use loosely(==
) comparison to compare the values. So they only check if the values are the same after PHP type juggling and without checking the type. Because of that in your example TRUE == "any none emtpy string"
evaluates to TRUE.
现在这意味着默认情况下两个函数都使用松散(==)比较来比较值。所以他们只检查PHP类型杂乱后的值是否相同,而不检查类型。因为在您的示例中,TRUE ==“any none emtpy string”的计算结果为TRUE。
So by setting the 3rd parameter to TRUE while calling the function you say that PHP should use strict(===
) comparison and it should check value AND type of the values while comparing.
因此,通过在调用函数时将第3个参数设置为TRUE,您说PHP应该使用严格(===)比较,并且它应该在比较时检查值的值和类型。
See this as a reference: How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
请参阅此参考:PHP等式(== double equals)和identity(=== triple equals)比较运算符有何不同?
#2
2
You are right, the boolean can indeed cause this. Set the strict
flag in the in_array
function, this way also the type of the element is checked (basically the same as using ===
):
你是对的,布尔确实可以导致这个。在in_array函数中设置strict标志,这样也检查了元素的类型(基本上与使用===相同):
if (in_array("username", $results, true)) // do something
if (in_array("password", $results, true)) // do something
if (in_array("birthday", $results, true)) // do something
#1
8
This behaviour of the function in_array()
and array_search()
is not a bug, but instead well documented behaviour.
函数in_array()和array_search()的这种行为不是错误,而是记录良好的行为。
Both functions have a 3rd optional parameter called $strict
which by default is FALSE:
这两个函数都有一个名为$ strict的第三个可选参数,默认情况下为FALSE:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
bool in_array(混合$ needle,数组$ haystack [,bool $ strict = FALSE])
mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )
mixed array_search(mixed $ needle,array $ haystack [,bool $ strict = false])
Now what that means is that by default both functions use loosely(==
) comparison to compare the values. So they only check if the values are the same after PHP type juggling and without checking the type. Because of that in your example TRUE == "any none emtpy string"
evaluates to TRUE.
现在这意味着默认情况下两个函数都使用松散(==)比较来比较值。所以他们只检查PHP类型杂乱后的值是否相同,而不检查类型。因为在您的示例中,TRUE ==“any none emtpy string”的计算结果为TRUE。
So by setting the 3rd parameter to TRUE while calling the function you say that PHP should use strict(===
) comparison and it should check value AND type of the values while comparing.
因此,通过在调用函数时将第3个参数设置为TRUE,您说PHP应该使用严格(===)比较,并且它应该在比较时检查值的值和类型。
See this as a reference: How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
请参阅此参考:PHP等式(== double equals)和identity(=== triple equals)比较运算符有何不同?
#2
2
You are right, the boolean can indeed cause this. Set the strict
flag in the in_array
function, this way also the type of the element is checked (basically the same as using ===
):
你是对的,布尔确实可以导致这个。在in_array函数中设置strict标志,这样也检查了元素的类型(基本上与使用===相同):
if (in_array("username", $results, true)) // do something
if (in_array("password", $results, true)) // do something
if (in_array("birthday", $results, true)) // do something