PHP的问题:strpos功能不起作用 [英] php problem: strpos function not working
问题描述
为什么以下php代码不起作用:
why is the following php code not working:
$string = "123";
$search = "123";
if(strpos($string,$search))
{
echo "found";
}else{
echo "not found";
}
由于$ search在$ string中-不应按找到的方式触发它?
as $search is in $string - shouldn't it be triggered as found?
推荐答案
此函数可以返回布尔FALSE,但也可以返回非布尔值,其值为FALSE,例如0或".请阅读有关布尔值的部分以了解更多信息.使用===运算符测试此函数的返回值.
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.
在您的情况下,该字符串位于索引0
和php 0 == false
In your case the string is found at the index 0
and in php 0 == false
解决方案是只使用严格的比较器
The solution is to just use the strict comparator
echo strpos($string,$search) === false
? "not found"
: "found";
另一个
echo is_int(strpos($string,$search))
? "found"
: "not found";
或者某事...让我们说些有趣的:D仅用于说明.我不推荐这个.
Or something ... lets say interesting :D Just for illustration. I don't recommend this one.
echo strpos('_' . $string,$search) // we just shift the string 1 to the right
? "found"
: "not found";
这篇关于PHP的问题:strpos功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!