php下使用strpos需要注意 === 运算符
程序员文章站
2022-08-14 09:05:34
复制代码 代码如下:
<?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
?>
复制代码 代码如下:
<?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
?>
上一篇: 大数据学习路线
下一篇: 网站盈利只需做好3件事