Today, I found a quite weird issue with php array_search function. Actually I applied the condition that, if index is 0 or greater than it should passed the IF condition otherwise not but its not functioning like that.
I analysed and found, if output is FALSE then ( FALSE >= 0) its also passing the condition with comparing value, don't know why. Can anyone explain this problem ?
It seems like not array_search function issue but i faced when using this function.
$allowedJobCodesForCC = array( "xyz", "abc");
/* output if value not found in array
var_dump(array_search(strtolower(trim('xyzfd')), $allowedJobCodesForCC));
*/
$output = array_search(strtolower(trim('xyz')), $allowedJobCodesForCC); //output : false
/* array_search function treating false return value and passing it to the condition */
if($output >= 0){
echo 'passed'; //it should not print this condition if return value is FALSE
}
/* correct fix if indexes are numeric */
if(is_numeric($output)){
echo 'passed';
}
PHP Manual : http://php.net/manual/en/function.array-search.php