php - 哪个更好的方法 null== 或 ==null [英] php - which one is better approach null== or ==null
本文介绍了php - 哪个更好的方法 null== 或 ==null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我多次看到 php 开发人员使用这样的东西:
I saw many times that php developers use somthing like this:
if(null == $var){
}
当我使用
if($var == null){
}
这两个有区别吗?有什么理由使用每一个吗?或者这在任何方面都是一个选择问题?
Are these two different? is there any reason to use each one? or it is a choice matter in any ways?
推荐答案
Simply null
不能赋值null = $a
,会抛出解析错误.
Simply null
can't be assigned value
null = $a
, it will throw parsing error.
但任何变量都可以将 null
作为值.$a = null
but any variable can have null
as value.
$a = null
因此,如果您在第二种情况下将 ==
错误输入为 =
,则您已更改了 $a
的值并检查是否$a
的新值的条件,它总是评估为假.
So if you mistyped ==
to =
in the second case, you have changed the value of $a
and you check the if condition of the new value of $a
which always evaluates to false.
这篇关于php - 哪个更好的方法 null== 或 ==null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文