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遍历所有文件及文件夹的方法深入解析

 1.方法一:复制代码 代码如下:<? $dir="D:"; static $dir_list =0; static $file_list =0; function li...

php中实现精确设置session过期时间的方法

大多数据情况下我们对于session过期时间使用的是默认设置的时间,而对于一些有特殊要求的情况下我们可以设置一下session过期时间。 对此,可以在PHP中,设置php.ini,找到s...

PHP分页显示制作详细讲解

每页多少条记录($PageSize)?   当前是第几页($CurrentPageID)?   现在只要再给我一个结果集,我就可以显示某段特定的结果出来。   至于其他的参数,比如:上一...

PHP实现获取某个月份周次信息的方法

本文实例讲述了PHP实现获取某个月份周次信息的方法。分享给大家供大家参考。具体如下: <?php function getMonthweeks($date){ $r...

简要剖析PHP的Yii框架的组件化机制的基本知识

简要剖析PHP的Yii框架的组件化机制的基本知识

组件是 Yii 应用的主要基石。是 yii\base\Component 类或其子类的实例。三个用以区分它和其它类的主要功能有: 属性(Property) 事件(Event)...