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

后端 未结 5 563
别跟我提以往
别跟我提以往 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:19

    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.)

提交回复
热议问题