Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 898
jergason Avatar asked Mar 30 '09 23:03

jergason


People also ask

What is the use of stripos() function in PHP?

The stripos() function finds the position of the first occurrence of a string inside another string. Note: The stripos() function is case-insensitive. Note: This function is binary-safe.

Is PHP strpos case sensitive?

strpos() Function: This function helps us to find the position of the first occurrence of a string in another string. This returns an integer value of the position of the first occurrence of the string. This function is case-sensitive, which means that it treats upper-case and lower-case characters differently.

How check return false in PHP?

The is_bool() function checks whether a variable is a boolean or not. This function returns true (1) if the variable is a boolean, otherwise it returns false/nothing.

What does Strpos return if not found?

Returns the position of the first occurrence of a string inside another string, or FALSE if the string is not found.


2 Answers

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

like image 163
Cody Caughlan Avatar answered Oct 22 '22 23:10

Cody Caughlan


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"
like image 23
Christian C. Salvadó Avatar answered Oct 22 '22 23:10

Christian C. Salvadó