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

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<?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
?>

相关文章

PHP高精确度运算BC函数库实例详解

本文实例讲述了PHP高精确度运算BC函数库。分享给大家供大家参考,具体如下: <?php /*************************************...

24条货真价实的PHP代码优化技巧

 PHP代码优化24条真经,希望对大家开发php项目有所帮助,具体内容如下  1.echo比print快。  2.使用echo的多重参数代替字符串连接。...

PHP几个实用自定义函数小结

本文实例总结了PHP几个实用自定义函数。分享给大家供大家参考,具体如下: 最近在看代码,发现以下是几个比较实用的函数。 1、取客户端IP function getOnlineIp()...

PHP实现的日历功能示例

PHP实现的日历功能示例

本文实例讲述了PHP实现的日历功能。分享给大家供大家参考,具体如下: <?php /* * Created by PhpStorm. * User: admin...

phpmyadmin config.inc.php配置示例

文件地址:D:\wamp\apps\phpmyadmin4.0.4\config.inc.php 文件内容: 复制代码 代码如下: <?php /* * Generated con...