php下使用strpos需要注意 === 运算符

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

<?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实现执行定时任务

PHP在这方面应该说是比较弱,如果只用php去实现可以如下: <?php ignore_user_abort();//关闭浏览器后,继续执行php代码 set_ti...

PHP内核探索:变量存储与类型使用说明

先回答前面一节的那个问题吧。 复制代码 代码如下:<?php    $foo = 10;    $b...

一个非常实用的php文件上传类

其实网上已经有很多这样的类了,不过出于练手的目的还是自己仿照着写了一个。 下面的代码放在一个名为UploadFile.class.php文件内 <?php /**...

PHP 面向对象实现代码

1,简单的对象创建 复制代码 代码如下: //类的作用域final:将阻止子类重写这个字段 //方法的作用域abstract: 在父类中声明,在子类中实现 //定义类: class Em...

让PHP开发者事半功倍的十大技巧小结

如果你使用一面大镜子作为冲浪板会发生什么?或许你会在较短的时间内征服海浪,但是你肯定从内心深处明白,这不是冲浪的正确选择。同样的道理也适用于PHP编程,尽管这样的类比听起来有一些古怪。我...