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根据日期显示所在星座的方法

本文实例讲述了php根据日期显示所在星座的方法。分享给大家供大家参考。具体实现方法如下: <?php function zodiac($DOB){ $DOB =...

从wamp到xampp的升级之路

准备学习php框架Laravel,但PHP版本要求5.4+,目前用的wamp默认版本5.3,所以准备使用xampp,以下为记录 卸载wamp,略过 > ps:注意卸载wamp会删除...

PHP中break及continue两个流程控制指令区别分析

以下举例说明break 用来跳出目前执行的循环,并不再继续执行循环了。 复制代码 代码如下: <?php $i = 0; while ($i < 7) { if ($arr[...

curl 出现错误的调试方法(必看)

实例如下: private function httpGet($url) { $curl = curl_init(); curl_setopt($curl,...

增加反向链接的101个方法 站长推荐

增加反向链接的101个方法 站长推荐

Andy Hagans和Aaron Wall写了一篇“增加反向链接的101个方法”,非常有参考价值。他们的帖子发出不到几小时,几乎所有重要的SEO bloggers都建议大家去看。这就是...