您现在的位置是:首页 > 网站建设常见问答网站建设常见问答
php下使用strpos需要注意 ===全等 运算符
2015-02-17【网站建设常见问答】2039 人已围观
简介今天我们跟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:
很赞哦! ()
上一篇:域名解析经常问到的问题
相关文章
随机图文
-
网站不能经常改版的原因
我们在制作网站的同时光考虑网站视觉上的效果,忽略网站文字上的作用是对seo网站优化推广的一特大坏处。网站改版任务我们在注重提升网站视... -
网页设计应该专注可用性和使用效果
使用对称的代码写法 构建响应式网站过程复杂,网站整体布局都应通过HTML和CSS代码库进行编码。在设计有关网页可用性的步骤时,强烈建议设计... -
双语网站中的外语翻译有谁提供?
外语版文字翻译由谁来准备? 外语版网站开发并不包含外语文字翻译工作,如果企业客户没有额外委托我们殷雯网络进行网站文案的翻译服务,则外语版的... -
文化局在我公司文艺演出
随后,文艺演出隆重举行,精彩的表演不断换来员工们的精彩表演。在表演节目的期间,还穿插了些有奖知识问答,在座的每位都是积极参与活动,整个场面十...