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原理之执行周期分析

本文讲述了PHP原理之执行周期。分享给大家供大家参考,具体如下: PHP的执行周期,从最初我们编写的PHP脚本->到最后脚本被执行->得到执行结果,这个过程,其实可以分为如下...

php版本CKEditor 4和CKFinder安装及配置方法图文教程

php版本CKEditor 4和CKFinder安装及配置方法图文教程

本文实例讲述了php版本CKEditor 4和CKFinder安装及配置方法。分享给大家供大家参考,具体如下: 下载并解压CKEditor 4和CKFinder CKEditor 4下载...

php加速器eAccelerator的配置参数、API详解

它还能够对脚本进行优化以便加速它们的执行速度。利用eAccelerator可以减少服务器的负载并提高PHP代码的执行时间。 eAccelerator配置选项:复制代码 代码如下:zend...

PHP实现找出有序数组中绝对值最小的数算法分析

本文实例讲述了PHP实现找出有序数组中绝对值最小的数算法。分享给大家供大家参考,具体如下: 问题: 一个有序数组,值有可能有负值,也有可能没有,现需要找出其中绝对值最小的值。 方法1:...

php将图片文件转换成二进制输出的方法

本文实例讲述了php将图片文件转换成二进制输出的方法。分享给大家供大家参考。具体实现方法如下: header( "Content-type: image/jpeg"); $PSize...