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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -