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设计模式 Bridge (桥接模式)

复制代码 代码如下: <?php /** * 桥接模式 * * 将抽象部份与它实现部分分离,使用它们都可以有独立的变化 */ abstract class Implementor...

php跨站攻击实例分析

本文实例讲述了php跨站攻击的原理与防范技巧。分享给大家供大家参考。具体方法分析如下: 跨站攻击就是利用程序上的一些细节或bug问题进行的,那么我们要如何耿防止跨站攻击呢?下面就以一个防...

PHP常见数组函数用法小结

本文实例讲述了PHP常见数组函数用法。分享给大家供大家参考,具体如下: 1.array array_merge(array $array1 [, array  $array2...

PHP学习笔记之一

配置PHP以及MySQL的过程略去,如果在Ubuntu下,参考 Ubuntu 10.04 Installation Memo 即可。 1. 基本语法 要在HTML代码中嵌入PHP脚本的方...

如何修改和添加Apache的默认站点目录

一、修改Apache的默认站点目录Apache HTTP Server安装好后,默认的站点目录位于其安装目录下的htdocs文件夹内,默认首页是该文件夹的index.html文件。比如,...