php strpos() 返回 0 和 false 之间的区别?

发布于 2024-08-23 23:19:22 字数 320 浏览 5 评论 0原文

if(strpos("http://www.example.com","http://www.")==0){ // do work}

我希望这能解决为真,事实确实如此。但是当我这样做时会发生什么

if(strpos("abcdefghijklmnop","http://www.")==0){// do work}

这也传递了 php 5,因为据我所知,strpos 返回 false,翻译为 0。

这是正确的想法/行为吗?如果是这样,测试子字符串是否位于另一个字符串开头的解决方法是什么?

if(strpos("http://www.example.com","http://www.")==0){ // do work}

I'd expect this to resolve as true, which it does. But what happens when I do

if(strpos("abcdefghijklmnop","http://www.")==0){// do work}

This also passes on php 5 because as far as I can work out the strpos returns false which translates as 0.

Is this correct thinking/behaviour? If so what is the workaround for testing for that a substring is at the beginning of another string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雾里花 2024-08-30 23:19:22

是的,这是正确/预期的行为:

  • 当字符串开头存在匹配时,strpos 可以返回 0
  • ,并且它将返回 false当没有匹配时

事情是你不应该使用 == 来比较 0false ;你应该使用===,像这样:

if(strpos("abcdefghijklmnop","http://www.") === 0) {

}

或者:

if(strpos("abcdefghijklmnop","http://www.") === false) {

}

有关详细信息,请参阅比较运算符

  • $a如果 $a 等于 $b,则 == $b 将为 TRUE
  • 如果 $a 等于 $b,并且 $a === $b 将为 TRUE >它们属于同一类型

并且,引用 strpos 的手册页:

该函数可能返回布尔值
FALSE,但也可能返回
非布尔值,其计算结果为
FALSE,例如 0""

阅读布尔值部分
更多信息。
使用===
用于测试返回的运算符

该函数的值。

Yes, this is correct / expected behavior :

  • strpos can return 0 when there is a match at the beginning of the string
  • and it will return false when there is no match

The thing is you should not use == to compare 0 and false ; you should use ===, like this :

if(strpos("abcdefghijklmnop","http://www.") === 0) {

}

Or :

if(strpos("abcdefghijklmnop","http://www.") === false) {

}

For more informations, see Comparison Operators :

  • $a == $b will be TRUE if $a is equal to $b.
  • $a === $b will be TRUE if $a is equal to $b, and they are of the same type.

And, quoting the manual page of strpos :

This function may return Boolean
FALSE, but may also return a
non-Boolean value which evaluates to
FALSE, such as 0 or "".
Please
read the section on Booleans for
more information.
Use the ===
operator
for testing the return
value of this function.

只是一片海 2024-08-30 23:19:22

===!== 比较类型值,如下所示:

if (strpos("abcdefghijklmnop", "http://www.") !== false) {
     // do work     
}

=== and !== compare type and value as shown below:

if (strpos("abcdefghijklmnop", "http://www.") !== false) {
     // do work     
}
风情万种。 2024-08-30 23:19:22

strpos 返回一个 int 或 boolean false。 == 运算符还将 0 计算为 false,您需要使用 === 运算符(三个等号)来检查要比较的类型是否相同,而不仅仅是查看它们是否可以被计算为表示相同。

所以

if (strpos($hastack, $needle) === 0)
{
    // the $needle is found at position 0 in the $haystack
}

strpos returns an int or boolean false. the == operator also evaluates 0 to mean false, you want to use the === operator (three equals signs) that also checks that the types being compared are the same instead of just seeing if they can be evaluated to mean the same.

so

if (strpos($hastack, $needle) === 0)
{
    // the $needle is found at position 0 in the $haystack
}
溺渁∝ 2024-08-30 23:19:22

0strpos 在开头找到匹配项时可能返回的值。如果未找到匹配项,则返回 false(布尔值)。因此,您需要使用 === 运算符检查 strpos 的返回值,该运算符检查值和类型,而不是使用 == 运算符只是检查值。

0 is a possible return value from strpos when it finds a match at the very beginning. In case if the match is not found it returns false(boolean). So you need to check the return value of strpos using the === operator which check the value and the type rather than using == which just checks value.

霊感 2024-08-30 23:19:22

我个人倾向于使用这种方式:

if(!strpos($v,'ttp:'))$v='http://'.$v;

或者

if(strpos(' '.$v,'http'))

避免使用“0”位置,然后始终将其设置为大于0的数字
干杯

I personally tend to use this way :

if(!strpos($v,'ttp:'))$v='http://'.$v;

or

if(strpos(' '.$v,'http'))

to avoid the "0" position then always make it a number more than 0
cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文