dovgqc2648 2016-12-02 15:20
浏览 50
已采纳

访问可以为null的对象字段的更好方法

I saw this line of code and I was asking myself the reason of this syntaxe

if(false && isset($_SESSION['new_user']) && $_SESSION['new_user'] == true)

Basically the context is if we have a new user we treat him differently than existing users.

My first guess is that is made to avoid search values in a null variable. So first he tests if there is a variable, and if there is he checks its value. Is that what he does?

But everything inside the same if statement.

My question is, if the first part of the if statement is false, the second part is not tested? Is this a good practice? or would be better to do like this:

if(($_SESSION['new_user']){
    if ($_SESSION['new_user'] == true){
        [...]
    }
}
  • 写回答

3条回答 默认 最新

  • duanqianwei2485 2016-12-02 15:25
    关注

    Your interpretation is correct. When using &&(AND), if the first condition is not met, no other conditions will be checked.

    So it doesn't matter how you format your if, as long as you understand how the code works.

    However, in your first example the false will make sure the condition is never true. In this case it is pretty useless.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害