您现在的位置是:首页 > 网站建设常见问答网站建设常见问答
php下使用strpos需要注意 ===全等 运算符
2015-02-17【网站建设常见问答】2242 人已围观
简介今天我们跟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:
很赞哦! ()
上一篇:域名解析经常问到的问题
相关文章
随机图文
不对称设计在网站建设中使用
不对称设计技术可以非常直观有趣和创建不同的焦点。 不对称仅仅是缺乏对称性,但不对称并没有缺乏平衡与和谐。 不对称和对称的设计都可以创...网站建设开始之域名的选择
在建设网站的时候,我们会面临很多的问题。比较常见的就是域名的选择。今天小编就来教教大家,如何选择一个好的域名。 现在网络营销已经成为了...网页中边框和底色设计技巧
本文是基础性的设计探讨文章,请大侠和高手绕道,菜鸟停留片刻。我们看过很多设计师的教程文章,大家都用分解的方法来说明设计的效果。 比如:一个...建站过程中网站服务器稳定与否与百度之间的关系
我们的公司企业在做好网站后,都需要放置到服务器上以供网络上的用户随时可以访问,这时就会有服务器稳定性的问题。如果企业网站放在了不稳定的机房...