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

yipeiwu_com6年前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全民k歌作品解析接口源码

<?php header("Access-Control-Allow-Origin:*"); header('Content-type: appli...

PHP使用strrev翻转中文乱码问题的解决方法

本文实例讲述了PHP使用strrev翻转中文乱码问题的解决方法。分享给大家供大家参考,具体如下: 在用PHP中的strrve翻转中文时,会出现乱码情况 例如: header("Con...

PHP实现MVC开发得最简单的方法——模型

昨天群里有人说使用MVC让程序多了很多数据库操作,使得性能下降,这着实让我吃了一惊。MVC只是一种框架,与数据库操作没有任何关系。MVC只是提供一种清晰的编程开发模式,只要你处理的好,是...

PHP基于curl模拟post提交json数据示例

本文实例讲述了PHP基于curl模拟post提交json数据。分享给大家供大家参考,具体如下: 这里php模拟post提交json数据操作的关键是在头部设置Content-Type...

PHP.ini安全配置检测工具pcc简单介绍

PHP.ini安全配置检测工具pcc简单介绍

概述 前一段时间,在工作中遇到了一个开源程序,该程序主要用来检测PHP配置文件中得配置项是否存在安全隐患,并提出相应的配置建议,使PHP程序更加安全。 使用 这个程序使用起来非常简单,大...