我正在使用 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==false ?当 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 Booleans for more information. Use the === operator for testing the return value of this function.
您必须使用 ===
运算符而不是 ==
.
在你的情况下,而不是使用 <>
, 使用 !==
:
strpos($grafik['data'], $ss1) !== false
如果 $ss1
,这将返回 TRUE在 $grafik['data']
中找到
关于php - 0==false 的 strpos 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4192025/