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.ini)

第一章  针对系统调用过多的优化 我这次的优化针对syscall调用过多的问题,所以使用strace跟踪apache进行分析。 1.  apache2ctl -X &...

一些 PHP 管理系统程序中的后门

我倒不怎么关心提示框,SABLOG怎么知道我的版本有漏洞呢,程序肯定有后门.每次登陆后台自动检测官方版本跟当前版本对比.嗯.后来找到了.在templates/admin/main.php...

简单谈谈PHP中的include、include_once、require以及require_once语句

1.include语句 使用include语句可以告诉PHP提取特定的文件,并载入它的全部内容 <?php inlude "fileinfo.php"; //此处添加...

php2html php生成静态页函数

<?php /** ------------------------ Function: php2html($in_Url, $out_htmlFile, $out_logFile...

php对csv文件的读取,写入,输出下载操作详解

复制代码 代码如下:<?php       $file = fopen('text.csv','r');   ...