arrays - What is wrong with the PHP function in_array(...)? -
this question has answer here:
the php function in_array(...) "checks if value exists in array".
but i'm observing strange behavior on handling strings (php v7.0.3). code
$needle = 'a'; $haystacks = [['a'], ['b'], [123], [0]]; foreach ($haystacks $haystack) { $needleisinhaystack = in_array($needle, $haystack); var_dump($needleisinhaystack); } generates following output:
bool(true) bool(false) bool(false) bool(true) <- what? the function returns true every string $needle, if $haystack contains element value 0!
is design? or bug , should reported?
if not set third parameter of in_array true, comparison done using type coercion.
if third parameter strict set true in_array() function check types of needle in haystack.
under loose comparison rules, 'a' equal 0 since (int)'a' == 0.
Comments
Post a Comment