php 5 strpos() 返回 0 和 false 之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2396713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php 5 strpos() difference between returning 0 and false?
提问by rutherford
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.
这也通过了 php 5,因为据我所知,strpos 返回 false,它转换为 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?
这是正确的想法/行为吗?如果是这样,测试子字符串是否位于另一个字符串的开头的解决方法是什么?
回答by Pascal MARTIN
Yes, this is correct / expected behavior :
是的,这是正确的/预期的行为:
strposcan return0when there is a match at the beginning of the string- and it will return
falsewhen there is no match
strpos可以0在字符串开头匹配时返回- 它会
false在没有匹配时返回
The thing is you should not use ==to compare 0and false; you should use ===, like this :
问题是你不应该==用来比较0和false;你应该使用===,像这样:
if(strpos("abcdefghijklmnop","http://www.") === 0) {
}
Or :
或者 :
if(strpos("abcdefghijklmnop","http://www.") === false) {
}
For more informations, see Comparison Operators:
有关更多信息,请参阅比较运算符:
$a == $bwill beTRUEif$ais equal to$b.$a === $bwill beTRUEif$ais equal to$b, and they are of the same type.
$a == $b将是TRUEif$a等于$b。$a === $b将是TRUEif$a等于$b,并且它们的类型相同。
And, quoting the manual page of strpos:
并且,引用手册页strpos:
This function may return Boolean
FALSE, but may also return a non-Boolean value which evaluates toFALSE, such as0or"".
Please read the section on Booleansfor more information.
Use the===operatorfor testing the return value of this function.
此函数可能返回 Boolean
FALSE,但也可能返回一个非布尔值,其计算结果为FALSE,例如0或""。
请阅读有关布尔值的部分以获取更多信息。
使用该===运营商的测试此函数的返回值。
回答by Ignacio Vazquez-Abrams
===and !==compare type andvalue as shown below:
===并!==比较类型和值,如下所示:
if (strpos("abcdefghijklmnop", "http://www.") !== false) {
// do work
}
回答by Kris
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.
strpos 返回 int 或 boolean false。== 运算符也将 0 计算为 false,您想使用 === 运算符(三个等号)来检查被比较的类型是否相同,而不是仅仅查看它们是否可以被评估为相同。
so
所以
if (strpos($hastack, $needle) === 0)
{
// the $needle is found at position 0 in the $haystack
}
回答by codaddict
0is a possible return value from strposwhen 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 strposusing the ===operator which check the value and the type rather than using ==which just checks value.
0是从strpos一开始就找到匹配项时的可能返回值。如果未找到匹配项,则返回false(布尔值)。因此,您需要strpos使用===检查值和类型的运算符来检查返回值,而不是使用==仅检查值的运算符。
回答by 3ème pilier
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 0cheers
为了避免“ 0”的位置,然后总是让它比0欢呼声更多

