gpt4 book ai didi

php - 为什么是 !== false 的 strpos 不是真的?

转载 作者:行者123 更新时间:2023-12-04 01:26:10 24 4
gpt4 key购买 nike

考虑以下示例:

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


如果没有找到针,它只返回一个 boolean 值。否则它将返回一个整数, including -1 and 0 ,随着针出现的位置。
如果你做过:
if(strpos($a,'is a') == true) {
echo 'True';
}
你通常会得到预期的结果,因为任何正整数都被认为是一个真值,并且因为当你使用 == 时类型杂耍运算符该结果为真。但是,如果字符串位于字符串的开头,则由于返回的零是假值,因此将等同于 false。

关于php - 为什么是 !== false 的 strpos 不是真的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52543130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com