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

前端之家收集整理的这篇文章主要介绍了php下使用strpos需要注意 === 运算符前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="97794" class="copybut" id="copybut97794" onclick="doCopy('code97794')"> 代码如下:

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

猜你在找的PHP相关文章