为什么 !== false 的 strpos 不正确?

why is a strpos that is !== false not true?

考虑以下示例:

$a='This is a test';

如果我现在这样做:

if(strpos($a,'is a') !== false) {
    echo 'True';
}

得到:

True

但是,如果我使用

if(strpos($a,'is a') === true) {
    echo 'True';
}

我一无所获。为什么在这种情况下 !==false 不是 ===true 我检查了 strpos() 上的 PHP 文档,但没有找到任何解释。

Because strpos() never returns true:

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

如果找不到针,它只是 return 一个布尔值。否则它将return一个整数,including -1 and 0,带有针出现的位置。

如果你这样做了:

if(strpos($a,'is a') == true) {
    echo 'True';
}

你通常会 得到预期的结果,因为任何正整数都被认为是真实值,并且因为当你使用 == 运算符时类型杂耍结果将是真实的. 但是 如果字符串在字符串的开头,它将等同于 false,因为零是 return,这是一个错误的值。

strpos 函数 return 成功时返回整数值,仅当未在字符串中找到针时返回 false。在我们的例子中,字符串 'This is a test' 包含 'is a'。所以 test (position)!==false [where position is the first occurrence of 'is a'] 在类型和值上都不同于 false,并且 return 为真。