zl程序教程

您现在的位置是:首页 >  后端

当前栏目

php下使用strpos需要注意===运算符

PHP 使用 需要 运算符 注意 strpos
2023-06-13 09:14:23 时间
复制代码代码如下:

<?php
/*
判断字符串是否存在的函数
*/
functionstrexists($haystack,$needle){
return!(strpos($haystack,$needle)===FALSE);//注意这里的"==="
}
/*
Test
*/
$mystring="abc";
$findme="a";
$pos=strpos($mystring,$findme);

//Noteouruseof===.Simply==wouldnotworkasexpected
//becausethepositionof"a"wasthe0th(first)character.
//简单的使用"=="号是不会起作用的,需要使用"===",因为a第一次出现的位置为0
if($pos===false){
echo"Thestring"$findme"wasnotfoundinthestring"$mystring"";
}else{
echo"Thestring"$findme"wasfoundinthestring"$mystring"";
echo"andexistsatposition$pos";
}

//Wecansearchforthecharacter,ignoringanythingbeforetheoffset
//在搜索字符的时候可以使用参数offset来指定偏移量
$newstring ="abcdefabcdef";
$pos =strpos($newstring,"a",1);//$pos=7,not0
?>