1

Why does example A fail, but example B work?

A) FAIL:

  $request_IP = '8.8.8.8';
  $list_Array = explode(',', "8.8.8.8,9.9.9.9,2.2.2.2");

  $result = array_search($request_IP, $list_Array);
  if($result) {
    // Expecting - to get it success there 
  }
  else {
    echo "FAIL";
    exit;          
  }

B) WORKS:

  $request_IP = '8.8.8.8';
  $list_Array = explode(',', "0,8.8.8.8,9.9.9.9,2.2.2.2");

  $result = array_search($request_IP, $list_Array);
  if($result) {
    // In this case it works??
  }
  else {
    echo "FAIL";
    exit;          
  }
mickmackusa
  • 43,625
  • 12
  • 83
  • 136

4 Answers4

1

From the documentation for array_search:

Returns the key for needle if it is found in the array, FALSE otherwise.

So you should change your code to:

$result = array_search($request_IP, $list_Array);
if ($result === false) {
    // Not found
    echo "FAIL";
    exit;
} else {
    // $result is the key of the element in the array
    echo $result . "\n";
}
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
1

Example A matches properly, it returns index 0.

0 is a falsy variable. 0 == false.

Look at https://secure.php.net/manual/en/types.comparisons.php what kind of values match to which result when doing loose comparisan(two ==) in the boolean column

You can better flip the if statements, to test for the false first. We don't need an "else" here because either the code will abort in the if statement, or it will continue.
Saves an indentation level.

if($result === false) {
    echo "FAIL";
    exit;          
}
// success

Three === means an exact match on value AND type

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
0

That's because in section A, array_search($request_IP, $list_Array) will return zero. so next condition line would compile as following:

if ($result)      // if (0)

this condition will always return false.

To fix this issue. you have to use following line

if ($result === FALSE)
James Lee
  • 1
  • 4
0

Because in your first example array_search will return 0 - because the first occurence of 8.8.8.8 is the first item. PHP is designed "weakly", so an assertion of "0" and boolean false will be true (false == 0) and an assertion of 1 and true will also be true (true == 1). This can be very tricky - as for example strpos returns 0 if a pattern is in the beginning of a string, in that case you would need to write your condition like (strpos($string, $search) !== false) - simply checking the return type "weakly" would result in a logic error (!strpos($string, $search).

You will have to adopt the condition.

I would recommend to use in_array

if (in_array($request_IP, $list_Array) === true)

will do

You should also have a look at this question: What are the benefits (and drawbacks) of a weakly typed language?

Weak types is not bad design, its concept, if you use PHP or any other weakly typed language for business logic, you will need to be extra careful, using strict comparison only (=== instead of ==, !== instead of != and explicit comparison instead of negation) is a good start.

Philipp Wrann
  • 1,751
  • 3
  • 19
  • 29