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中利用sleep函数实现定时执行功能实现代码

在一些竞猜的网站中,如果我们需要做一个定时执行的功能,比如有一道题,在十秒之内要完成,否则显示“您已超时”,如果完成,则跳转到下一道题上面,而这中间有一个十秒的停顿,这样的功能是怎样实现...

php strnatcmp()函数的用法总结

Definition and Usage定义和用法The strnatcmp() function compares two strings using a "natural" algo...

php number_format() 函数通过千位分组来格式化数字的实现代码

定义和用法number_format() 函数通过千位分组来格式化数字。 语法number_format(number,decimals,decimalpoint,separator)...

PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】

本文实例讲述了PHP的图像处理。分享给大家供大家参考,具体如下:1、添加文字水印//1、打开图片资源   $src="./material/sea.jpg&...