您现在的位置是:首页 > 网站建设常见问答网站建设常见问答
php下使用strpos需要注意 ===全等 运算符
2015-02-17【网站建设常见问答】2140 人已围观
简介今天我们跟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:
很赞哦! ()
上一篇:域名解析经常问到的问题
相关文章
随机图文
web虚拟主机的"月流量"是什么意思?
网站客户在购买网站虚拟主机后,\"月流量2G\",那每天是允许多少个用户访问呢? 答:如果没有下载内容,仅仅是文章,应该没多...PHP图像操作GD库学习一
在我们开发网站中,用php对图片进行操作一般很少见。殷雯工作室一般只用到GD库对验证码或者用户上传的产品图片加个水印。企业网站中...网站建设的最终目的
为什么我们要做网站呢?这个问题拿今天来说应该是相当简单来回答的。 众所周知,做网站不在乎做网站的外表(外表是做给自己看) 看中的是...殷雯jquery笔记:删除HTML元素
jQuery使用下面两个方法来删除或是清空某个HTML元素。 remove() – 删除指定的元素(包括其子元素) empty() – ...