加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – strpos问题0 == false?

发布时间:2020-12-13 22:21:34 所属栏目:PHP教程 来源:网络整理
导读:我正在使用strpos在另一个字符串中找到字符串的位置.我首先检查那里是否找到了字符串.这是我的路线: if (strpos($grafik['data'],$ss1)false strpos($grafik['data'],$ss2)false strpos($grafik['data'],$ss1) strpos($grafik['data'],$ss2)) 我检查两个字
我正在使用strpos在另一个字符串中找到字符串的位置.我首先检查那里是否找到了字符串.这是我的路线:

if (strpos($grafik['data'],$ss1)<>false && strpos($grafik['data'],$ss2)<>false && strpos($grafik['data'],$ss1) < strpos($grafik['data'],$ss2))

我检查两个字符串是否都包含在内,然后我希望第一个字符串放在第二个字符串之前.在php手册中,它表示当找不到字符串时strpos返回false.但是如果我的字符串从零开始(strpos从开始就返回0),它就像是这个语句

strpos($grafik['data'],$ss1)<>false

是假的.莫名其妙0 ==假?当strpos返回0时,如何使语句成立?

解决方法

从 http://www.php.net/manual/en/function.strpos.php开始:

Warning

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 07001 for more
information. Use 07002 for
testing the return value of this
function.

您必须使用===运算符而不是==.

在您的情况下,不要使用<>,而是使用!==:

strpos($grafik['data'],$ss1) !== false

如果在$grafik [‘data’]中找到$ss1,则返回TRUE

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读