在php中使用==和===

时间:2012-09-25 10:05:25

标签: php comparison-operators

  

可能重复:
  What does “===” mean?

我对在php中使用那些运算符感到困惑,我不太确定我什么时候应该使用===和==。

例如为什么/何时应该写:

if( $some_method_that_returns_something_or_false() === FALSE) {
      //do stuff
}

以及何时使用==?

另外,===表示我必须返回bool FALSE或者我可以返回0吗?当使用===或==?

被认为是不好的做法时

同样放这样的东西:

 if($some_method_that_returns_true_or_false()) {

 }

是$ some_method_that_returns_true_or_false()== TRUE或 some_method_that_returns_true_or_false()===是吗?

2 个答案:

答案 0 :(得分:3)

===表示精确值,因此对于true,它必须为true,而==检查值的含义,因此true也将是值'1'或任何String。

答案 1 :(得分:1)

==用于检查相等性,===用于检查相等性和类型。

if($some_method_that_returns_true_or_false()) {

}

正在检查$some_method_that_returns_true_or_false() == TRUE