gpt4 book ai didi

PHP 问题 : strpos function not working

转载 作者:可可西里 更新时间:2023-11-01 13:39:24 24 4
gpt4 key购买 nike

为什么下面的 php 代码不起作用:

$string = "123";
$search = "123";

if(strpos($string,$search))
{
echo "found";
}else{
echo "not found";
}

因为 $search 在 $string 中 - 它不应该在找到时触发吗?

最佳答案

Manual: 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.

在你的例子中,字符串在索引 0 和 php 0 == false 中找到

解决方案是只使用严格比较器

echo strpos($string,$search) === false
? "not found"
: "found";

另一个

echo is_int(strpos($string,$search))
? "found"
: "not found";

或者某事...让我们说有趣 :D 仅供说明。我不推荐这个。

echo strpos('_' . $string,$search) // we just shift the string 1 to the right
? "found"
: "not found";

关于PHP 问题 : strpos function not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6099309/

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