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

Ascaliko
  • 76
  • 1
  • 9
  • 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. – fubar Oct 09 '18 at 06:02
  • what does `echo $output` gives? – Exterminator Oct 09 '18 at 06:03
  • If you try with the value which does not exists then will return FALSE. You can try by using var_dump. – Senior PHP Developer Oct 09 '18 at 06:05
  • 2
    Okay, but when using loose comparison checks, `0 == false -> true`, whereas `0 === false -> false`. – fubar Oct 09 '18 at 06:07
  • The php manual page you’ve linked has a warning box explaining the perils of the possible return values – Claire Furney Oct 09 '18 at 06:14
  • @fubar : I knew the condition with this case should be handle differently but why false >=0 is passing the condition ? – Senior PHP Developer Oct 09 '18 at 06:20
  • `$value >= 0` could be rewritten as `$value > 0 || $value == 0`. In this case, if `$value` is `false`, `false == 0` is `true`. – fubar Oct 09 '18 at 06:34
  • @fubar : So you are trying to say that its a bug in php library ? which is treating FALSE same as 0. – Senior PHP Developer Oct 09 '18 at 06:44
  • No, definitely not. PHP is a loosely typed language. `0` is falsy, so this is expected behaviour. Just use strict comparisons, per my previous comments. – fubar Oct 09 '18 at 06:58
  • @KuldeepSingh Kindly go through this link https://stackoverflow.com/questions/137487/null-vs-false-vs-0-in-php hoping it will be helpful. – ABHI Oct 09 '18 at 07:08

2 Answers2

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.

Arthur Shveida
  • 447
  • 2
  • 8
  • Its seems correct but still have a question in mind, Is that a logical or a PHP bug ? – Senior PHP Developer Oct 09 '18 at 07:32
  • 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 – Arthur Shveida Oct 09 '18 at 07:43
  • 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 – Arthur Shveida Oct 09 '18 at 07:56
  • 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 '=='; } – Senior PHP Developer Oct 10 '18 at 04:31
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';
}
Claire Furney
  • 2,115
  • 3
  • 24
  • 36
Exterminator
  • 1,221
  • 7
  • 14