Home  >  Article  >  Backend Development  >  浅析php中三个等号(===)和两个等号(==)的区别_php技巧

浅析php中三个等号(===)和两个等号(==)的区别_php技巧

WBOY
WBOYOriginal
2016-05-17 08:56:251181browse

先举个列子:
比如你一个函数会返回这几种情况:
1、大于0的数
2、小于0的数
3、等于0的数(也就是0啦)
4、False(失败时)
这时候如果你想捕获失败的情况,你就必须用===,而不能用==
因为==除了会匹配第4种情况外,还会匹配第3种情况,因为0也是假!

三个等号代表比较对象的类型也要一致。两个等号表示只要值相等就满足条件。

再来补充一些:
$a='2';//字符型2
$b=2;//数值型2
$a==$b,是对的,都是2
$a===$b,是不对的,因为$a是字符型$b是数值型,值虽一样,但类型不一样。
还有就是“linvo1986 - 六级”说的那种“0”了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn