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

后端 未结 5 562
别跟我提以往
别跟我提以往 2020-12-10 11:32

Here is the code I am looking at.

foreach ($header as $idx => $field) {
    if (stripos($field, \'foo\') !== false) {
        $cols[\'foo\'] = $idx;
             


        
5条回答
  •  悲哀的现实
    2020-12-10 12:06

    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"
    

提交回复
热议问题