您现在的位置是:首页 > 网站建设常见问答网站建设常见问答
php下使用strpos需要注意 ===全等 运算符
2015-02-17【网站建设常见问答】2398 人已围观
简介今天我们跟strpos函数较真一下,首先应该知道 strpos 函数可能返回布尔值 FALSE,但也可能返回一个与 FALSE 等值的非布尔值,例如 0 或者\...
今天我们跟strpos函数较真一下,首先应该知道 strpos 函数可能返回布尔值 FALSE,但也可能返回一个与 FALSE 等值的非布尔值,例如 0 或者\"\"。我们应使用 === 运算符来测试本函数的返回值。
例如“ strpos(\"a\",\"abc\"); 返回的是0 计位是从0开始。 0等于同于false
这时===号就可以派上用场
<?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
?>
/*
判断字符串是否存在的函数
*/
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
?>
关注宁波网站建设博客,更多精彩分享,敬请期待!
Tags:
很赞哦! ()
上一篇:域名解析经常问到的问题
相关文章
随机图文
网站维护、网站托管、安全、可靠
我们先来估算一下网站维护的成本。网站制作完成后需要维护与管理,得请一个有一定网络技术的人负责。请懂一定网络技术的人员每月的工资最低标准为3...mysql常见错误代码及解决办法
制作php+mysql的动态网站,数据库是经常需要打交道的事情。mysql一些常见的错误代码如下 130 :文件格式不正确。(还不是很清楚...怎么设计一个漂亮的企业网站
打开一个网站第一眼就是他的门面,给客户的第一感觉,这很重要,为将来促成交易做下一个基础。那怎么样做一个漂亮的企业网站呢?那第一眼就很重要...网页中边框和底色设计技巧
本文是基础性的设计探讨文章,请大侠和高手绕道,菜鸟停留片刻。我们看过很多设计师的教程文章,大家都用分解的方法来说明设计的效果。 比如:一个...