加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

PHP为什么(null === $variable)而不是($variable === null)比较

发布时间:2021-07-06 05:22:11 所属栏目:大数据 来源: https://www.jb51.cc
导读:参见英文答案 Is there any benefit of using null first in PHP?????????????????????????????????????5个 在阅读Symfony2代码时,我多次遇到这个比较 if (null === $variable ) { ... } 我用 if($variable === null){…} 因为我觉得它更具可读性. 使用第一

参见英文答案 > Is there any benefit of using null first in PHP?????????????????????????????????????5个
在阅读Symfony2代码时,我多次遇到这个比较

if (null === $variable ) { ... }

我用

if($variable === null){…}

因为我觉得它更具可读性.

使用第一种符号背后有智慧吗?

解决方法:

它有助于防止意外分配:

if ($foo = null) { ... }

会不会导致解析错误

if (null = $foo) { ... }

第一个例子:http://sandbox.onlinephpfunctions.com/code/fff14c285a18a7972a3222ff5af08da825760c10
第二个例子:http://sandbox.onlinephpfunctions.com/code/118d494c17381d8e129ba005465bf84d9b8819bd

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读