您现在的位置是:首页 > 网站建设常见问答网站建设常见问答
php下使用strpos需要注意 ===全等 运算符
2015-02-17【网站建设常见问答】2209 人已围观
简介今天我们跟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:
很赞哦! ()
上一篇:域名解析经常问到的问题
相关文章
随机图文
dedecms如何添加自定义函数
最近本工作室开始使用dedecms二次开发网站,很多功能需要重新学习一下。幸好网上关于dedecms二次开发网站建设的例子不少。今天我们...mysql常见错误代码及解决办法
制作php+mysql的动态网站,数据库是经常需要打交道的事情。mysql一些常见的错误代码如下 130 :文件格式不正确。(还不是很清楚...双语网站中的外语翻译有谁提供?
外语版文字翻译由谁来准备? 外语版网站开发并不包含外语文字翻译工作,如果企业客户没有额外委托我们殷雯网络进行网站文案的翻译服务,则外语版的...建站过程中网站服务器稳定与否与百度之间的关系
我们的公司企业在做好网站后,都需要放置到服务器上以供网络上的用户随时可以访问,这时就会有服务器稳定性的问题。如果企业网站放在了不稳定的机房...