PHP为什么要比较(null === $ variable)而没有($ variable === null)? [英] PHP why (null === $variable) and not ($variable === null) in comparison?
问题描述
在阅读Symfony2
代码时,我多次遇到了这种比较
if (null === $variable ) { ... }
我使用
if ($variable === null ){ ... }
因为我认为它更具可读性.
使用第一种表示法后面有智慧吗?
它有助于防止意外分配:
if ($foo = null) { ... }
不会导致解析错误,而
if (null = $foo) { ... }
第一个示例: http://sandbox.onlinephpfunctions.com/code/fff14c285a18a7972a3222ff5af08da825c10
第二个示例: http://sandbox.onlinephpfunctions.com/code/118d494c17381d8e129ba005465bf84d9b8819bda When reading I use because I see it more readable. Is there a wisdom behind using the first notation ? It helps prevent accidental assignments: would not cause a parse error, while will 1st example: http://sandbox.onlinephpfunctions.com/code/fff14c285a18a7972a3222ff5af08da825760c10
2nd example: http://sandbox.onlinephpfunctions.com/code/118d494c17381d8e129ba005465bf84d9b8819bd 这篇关于PHP为什么要比较(null === $ variable)而没有($ variable === null)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!Symfony2
code I came across this comparaison many times if (null === $variable ) { ... }
if ($variable === null ){ ... }
if ($foo = null) { ... }
if (null = $foo) { ... }