I have found some odd behaviour while I was using the PHP function in_array()
. I have an array like this:
$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?
$result = in_array("test", $arr);
var_dump($result); //Output: bool(true)
The same thing happens when using 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.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…