为什么strpos是!== false而不是true?

为什么strpos是!== false而不是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文档,但未对此找到任何解释.

I get nothing. Why is !==false not ===true in this context I checked the PHP docs on strpos() but did not find any explanation on this.

因为永远不会返回true :

返回针相对于干草堆弦的起点的位置(与偏移量无关).另外请注意,字符串位置从0开始,而不是1.

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.

如果找不到针,则返回FALSE.

Returns FALSE if the needle was not found.

仅在找不到针的情况下才返回布尔值.否则,它将返回一个整数,包括-1和0 ,以及发生针的位置.

It only returns a boolean if the needle is not found. Otherwise it will return an integer, including -1 and 0, with the position of the occurrence of the needle.

如果您已完成:

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

您通常会获得预期结果,因为任何正整数都被视为真实值,并且因为在使用==运算符时键入杂耍,结果将为true. 但是如果该字符串位于字符串的开头,则由于返回零(这是一个falsey值),因此它等同于false.

You would have usually gotten expected results as any positive integer is considered a truthy value and because type juggling when you use the == operator that result would be true. But if the string was at the start of the string it would equate to false due to zero being return which is a falsey value.