首页 > IT职业 > IT生活 > PHP的strpos函数辨析
由于strpos在找不到内容时返回FALSE,因此,语法:
1
2
3
if(strpos($str,$find)==0){
if
(
strpos
$str
,
$find
)==0){
//...
实际是想在找到且位置为0时进入,结果如果找不到也会进入。 }
实际是想在找到且位置为0时进入,结果如果找不到也会进入。
}
在找不到时总会进入条件为真的逻辑,因为PHP里FALSE==0为真。
应修改为:
if(strpos($str,$find)===0){
)===0){
类似的不少函数返回值,使用===更安全和准确。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1075/viewspace-2801598/ ,如需转载,请注明出处,否则将追究法律责任。
注册时间:2019-04-15