在PHP中,两个等号==表示值相同的判断,而三个等号===表示值和类型都要相同。
比如PHP中的strpos函数,如果在字符串中没有找到要找的子串,函数返回false,如果子串出现在最开始的地方,函数返回0。这个时候,就要判断,函数的返回值到底是0还是false,如下示例代码:
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// 注意这里使用的是 ===。简单的 == 不能像我们期待的那样工作,
// 因为 '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";
}
再来补充一些:
$a='2';//字符型2
$b=2;//数值型2
$a==$b;//是true,都是2
$a===$b;//是false,因为$a是字符型$b是数值型,值虽一样,但类型不一样。
现在搞清楚PHP中两个等号==和三个等号===的区别了吧。