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开发之归档格式phar文件概念与用法详解【创建,使用,解包还原提取】

本文实例讲述了PHP开发之归档格式phar文件概念与用法。分享给大家供大家参考,具体如下: 一个php应用程序往往是由多个文件构成的,如果能把他们集中为一个文件来分发和运行是很方便的,这...

php简单截取字符串代码示例

本文实例讲述了php简单截取字符串的方法。分享给大家供大家参考,具体如下: //截取摘要 public static function mbsubstr($str){ $strl...

php实现QQ空间获取当前用户的用户名并生成图片

本文实例讲述了php实现QQ空间获取当前用户的用户名并生成图片的方法。分享给大家供大家参考。具体如下: 最近发现空间里经常会转载一些含有当前用户昵称和qq号的图片,很好奇,研究了一下原理...

PHP 检查扩展库或函数是否可用的代码

本文介绍的函数其实是PHP手册上本来就有的,但是由于这些函数独立性较强,查找不易,所以单独介绍一下,方便查阅。 1. 获取所有可用的模块 - get_loaded_extensions...

解决php中Cannot send session cache limiter 的问题的方法

今天在使用php 的session 的时候,出现了以前就遇见但是又解决不了的问题,在页面上出现如下提示: Warning: session_start()&...