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

相关文章

apache php模块整合操作指南

apache php模块整合操作指南

apache的版本: httpd-2.2.21-win32-x86-no_ssl php的版本: php-5[1].3.8-Win32-VC9-x86 (一) 准备工作 1.先找在D:/...

浅析php-fpm静态和动态执行方式的比较

本文实例讲述了php-fpm静态和动态执行方式的比较。分享给大家供大家参考,具体如下: 前段时间配置php-fpm的时候,无意间发现原来他还有两种执行方式。与Apache一样,他的进程数...

PHP利用REFERER根居访问来地址进行页面跳转

比如,我有一个开发一个黄页源码上转到了【宜配屋www.yipeiwu.com】。之前定了一个演示程序地址: //www.jb51.net 而现在这个域名需要用来作其它的站,又不杀望原来的...

php通过sort()函数给数组排序的方法

本文实例讲述了php通过sort()函数给数组排序的方法。分享给大家供大家参考。具体分析如下: sort()函数用于给数组排序,本函数为数组中的单元赋予新的键名。原有的键名将被删除。...

php5.4传引用时报错问题分析

本文实例分析了php5.4传引用时报错问题。分享给大家供大家参考,具体如下: php5.3系列版本以及以前版本,传引用没有什么问题,升级到php5.4以后,传引用的地方,全报错 Fata...