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

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

发布时间:2020-12-13 05:45:41 所属栏目:PHP教程 来源:网络整理
导读:div class="codetitle" a style="CURSOR: pointer" data="97794" class="copybut" id="copybut97794" onclick="doCopy('code97794')" 代码如下: div class="codebody" id="code97794" ?php / 判断字符串是否存在的函数 / function strexists($haystack,$need

<div class="codetitle"><a style="CURSOR: pointer" data="97794" class="copybut" id="copybut97794" onclick="doCopy('code97794')"> 代码如下:<div class="codebody" id="code97794">
<?php
/
判断字符串是否存在的函数
/
function strexists($haystack,$needle) {
return !(strpos($haystack,$needle) === FALSE);//注意这里的"==="
}
/
Test
/
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring,$findme); // Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
// 简单的使用 "==" 号是不会起作用的,需要使用 "===",因为 a 第一次出现的位置为 0
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
} // We can search for the character,ignoring anything before the offset
// 在搜索字符的时候可以使用参数 offset 来指定偏移量
$newstring = 'abcdef abcdef';
$pos = strpos($newstring,'a',1); // $pos = 7,not 0
?>

(编辑:李大同)

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

    推荐文章
      热点阅读