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的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法

php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法

最近遇到一个问题,就是在使用php的mail函数发送utf-8编码的中文邮件时标题出现乱码现象,而邮件正文却是正确的。最初以为是页面编码的问题,发现页面编码utf-8没有问题啊,找了半天...

PHP实现防盗链的方法分析

本文实例讲述了PHP实现防盗链的方法。分享给大家供大家参考,具体如下: $_SERVER['HTTP_REFERER']的获取情况 注意 $_SERVER['HTTP_REFERER']...

PHP内核探索:变量概述

现代编程语言中的基本元素主要有:变量,流程控制接口,函数等等。我能否不使用变量来编写程序呢? 这显然是可以的,例如: 复制代码 代码如下:<?php  &nbs...

谈谈PHP连接Access数据库的注意事项

谈谈PHP连接Access数据库的注意事项

首先需要注意: 安装access 数据库的时候 需要安装与本机系统相互匹配的office版本,win7 64位的系统 ,那么Office也要是64位的 最好装 office2010。。。...

PHP面向对象继承用法详解(优化与减少代码重复)

本文实例讲述了PHP面向对象继承用法。分享给大家供大家参考,具体如下: 继承 先看两个类 <?php class CdProduct { public $playL...