Jamison Dance
Jamison Dance

Reputation: 20184

Why use !== FALSE to check stripos in php?

Here is the code I am looking at.

foreach ($header as $idx => $field) {
    if (stripos($field, 'foo') !== false) {
        $cols['foo'] = $idx;
    } else if (stripos($field, 'bar') !== false) {
        $cols['bar'] = $idx;
    } else if (stripos($field, 'brr') !== false) {
        $cols['brr'] = $idx;
    } else if (stripos($field, 'ffo') !== false) {
        $cols['ffo'] = $idx;
    }
}

Sorry, don't know how to format the code prettily either, any tips on that would be appreciated.

I am looking at some code written by someone much smarter than I, so I am not inclined to trust my first impression to just change everything to if(stripos($foo)), but why do it this way?

Upvotes: 18

Views: 39528

Answers (5)

OIS
OIS

Reputation: 10033

Dunno if something like this would be easier to maintain. Depends if you do something else in those if conditions. But you can set the keys in a config file, db, pass as argument to your function, or similar.

$keys = array(
    'foo',
    'bar',
    'brr',
    'ffo',
);
foreach ($header as $idx => $field) {
    foreach ($keys as $key) {
        if (stripos($field, $key) !== false) {
            $cols[$key] = $idx;
            break;
        }
    }
}

Upvotes: 3

matpie
matpie

Reputation: 17512

Because if the string matched at position 0, 0 evaluates to false, so you make sure to add the extra '=' so that type is taken in to consideration.

strpos() returns false when NO match is found.

Check out comparison operators.

Upvotes: 6

karim79
karim79

Reputation: 342655

In PHP, !== means not of the same type AND value.

If stripos() returns anything other than false (and exactly 'false', and not zero), it means it has found something, even if the position is 0 and int(0) is returned. 0 and false are equal when doing a standard comparison with the == but not when using the identity === operator, so the only way to know if stripos() has found something for certain is to compare false's value and type using !== (not identical, ie of both the same type and value of strpos()'s return value.)

Upvotes: 6

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827496

stripos returns the position of a string inside another, and if the string is not found, it returns false, so it's recommended to use the identity comparison operators (===, !==), because PHP considers 0 as a "falsy" value, consider this example:

// Find the position of the 'a' character in the 'abc' string:
stripos('abc', 'a') !== false; // true, position is 0
stripos('abc', 'a') != false; // false, 0 is "falsy"

Upvotes: 10

Cody Caughlan
Cody Caughlan

Reputation: 32758

The answer is that in PHP a "false" value can be satisfied by a handful of values, such as an empty array, an empty string, a NULL, integer 0, etc. See the empty() function page for a full list:

http://php.net/empty

So this would always yield incorrect results:

if(strpos("abc", "a")) { 
  echo "Yes";
} else {
  echo "No";
}

Since the "a" occurs at the first position (index 0) then PHP considers "if (0)" to be false.

When strpos does NOT find the needle in your haystack it will return the boolean FALSE, which is what you want to check with the triple-equal operator which checks both type and value. See the docs on comparison operators

http://www.php.net/manual/en/language.operators.comparison.php

Upvotes: 51

Related Questions