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获取不到SESSION信息之一般情况

一、检查 php.ini vim /etc/php.ini 检查这几项: session.save_handler = files // session 的存储类型; se...

php通过ajax实现双击table修改内容

复制代码 代码如下: <script type="text/javascript"> $(function() { $("td").dblclick(function() {...

php中拷贝构造函数、赋值运算符重载

对象的赋值与复制: 赋值:通过“ = ”运算符重载User a(10),b;b = a;复制:调用复制构造函数User b;User a(b);或者User a = b;//相当于Use...

php生成动态验证码gif图片

php生成动态验证码gif图片

这是一个通过php生成的动态验证码图片的示例,重点是可以运行哦!下面先发下效果图: 下面是php生成动态验证码需要用到的相关类和函数。 <?php /** *...

php获取文件类型和文件信息的方法

本文实例讲述了php获取文件类型和文件信息的方法。分享给大家供大家参考。具体实现方法如下: <?php $file = "php.txt"; //打开文件,r表示以只读...