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 在5.1.* 和5.2.*之间 PDO数据库操作中的不同之处小结

介绍 今天发现php5.1.*和php5.2.*在数据库预编译代码执行的时候出现差异。 预编译优点 1.使用占位符,避免逐字输入数据到SQL中。自动处理引号和反斜线等字符的转义——增加安...

用php实现像JSP,ASP里Application那样的全局变量

复制代码 代码如下:<?php /**  * 功能:实现像JSP,ASP里Application那样的全局变量  * author:&nbs...

Yii2——使用数据库操作汇总(增删查改、事务)

本文介绍了 Yii2——使用数据库操作汇总(增删查改、事务),具体如下: 对象操作 查询 //1.简单查询 $admin=Admin::model()->findAll...

PHP使用new StdClass()创建空对象的方法分析

本文实例讲述了PHP使用new StdClass()创建空对象的方法。分享给大家供大家参考,具体如下: PHP可以用 $object = new StdClass(); 创建一个没有成员...

php连接mssql的一些相关经验及注意事项

为了能让PHP连接MSSQL,系统需要安装MSSQL,PHP,且在PHP.ini中的配置中,将 ;extension=php_mssql.dll前面的;去掉 1.连接MSSQL 复制代码...