4

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

10
  • 6
    It's not returning false. array_search returns the index at which the search value is found, or false when not found. The return value in your example is 0. 0 !== false. TL;DR - use strict comparison for the return value. Commented Oct 9, 2018 at 6:02
  • what does echo $output gives? Commented Oct 9, 2018 at 6:03
  • If you try with the value which does not exists then will return FALSE. You can try by using var_dump. Commented Oct 9, 2018 at 6:05
  • 2
    Okay, but when using loose comparison checks, 0 == false -> true, whereas 0 === false -> false. Commented Oct 9, 2018 at 6:07
  • The php manual page you’ve linked has a warning box explaining the perils of the possible return values Commented Oct 9, 2018 at 6:14

2 Answers 2

2

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 ?

Take a look at Comparison with Various Types table at http://php.net/manual/en/language.operators.comparison.php

According to this table, if you compare boolean with any other type, both values are converted to boolean and then compared. In your case integer 0 is converted to FALSE and eventually php compares FALSE >= FALSE. Since FALSE is greater or equal to FALSE you condition returns true.

Sign up to request clarification or add additional context in comments.

4 Comments

Its seems correct but still have a question in mind, Is that a logical or a PHP bug ?
It's the intended and documented behavior, so definitely not a PHP bug. PHP is loosely typed language, so developers had to find a way to compare different types. That's just how they handled it
Logically, if you actually do compare boolean with int, I'd say that this behavior is quite logical. But comparison itself does not make much sense. However question if it's logical or not seems more like opinion-based
I did little research on it with javascript and its behaving same with boolean value (false) but not behaving same with string value. /* Javascript Code / var test = 'sdf'; if(test >= 0){ alert('hi'); } / PHP Code */ $test = 'sdf'; if($test >= 0){ echo '=='; }
0

You need to use === as it checks the value and also checks for the type of the value so that it won't pass the condition as it was happening in your case. It was checking for the value but wasn't checking its type which was creating the problem as it was treating false as a string which is obviously true condition (value of string is greater than 0).

$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 === False && $output !== 0) {
    echo 'not passed'; //it should not print this condition if return value is FALSE
} else {
    echo 'passed';
}

8 Comments

I know the solution but i am asking why its happening ?
as it it return false you are comparing using == which only checks the value and here in your case false is treated as a string and any string > 0 always that's why
I did not mentioned == to anywhere.
>= i mean this one only, it is checking the value
you can do var_dump to check datatype. Its returning boolean not string type data.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.