문제

Question, how come the following executed the echo:

$str = "Hello World";

if (strpos($str, 'He') !== false) {
    echo 'GOOD';
}

But this doesn't:

$str = "Hello World";

if (strpos($str, 'He') === true) {
    echo 'GOOD';
}

Aren't the two conditions equivalent in that they are both checking for the returned to be a boolean that is set to true? Isn't !== false the same as === true, and if not, why not?

I appreciate the clarification.

도움이 되었습니까?

해결책

No they're not equivalent:

strpos() returns either boolean FALSE (if not found) or an integer offset value (which can be 0 if found at offset 0 and so on), but it never returns a boolean TRUE. ie., Boolean TRUE !== an INT.

다른 팁

The operator === compares not only value but also a datatype. If strpos finds the substring, it returns the position which is of type int. As it is not bool, the condition is not met.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top