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获取当前url的具体方法全面解析

我们今天向大家介绍的是有关PHP获取当前url的函数的具体方法,通过对这个函数方法的了解,进一步加深我们对PHP语言的认识,提高我们的学习水平。 PHP 5.0构造函数的实例讲解 PH...

PHP中开启gzip压缩的2种方法

网页开启gzip压缩以后,其体积可以减小20%~90%,可以节省下大量的带宽,从而减少页面响应时间,提高用户体验。 php配置改法: 复制代码 代码如下: zlib.output_com...

php打开文件fopen函数的使用说明

1.resource  fopen(string  $filename, string $mode [,bool $use_include_path [, resou...

php foreach循环中使用引用的问题

看代码,再做解释复制代码 代码如下:<?php $array=array('a','b','c','d'); foreach($array as $key=>$val){ &...

判断php数组是否为索引数组的实现方法

HP没有内置判断是否索引数组的方法,简单实现了一个,用法:复制代码 代码如下:echo is_assoc($array)?'索引数组':'不是索引数组';is_assoc函数如下:复制代...