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条)

报告相同问题?

悬赏问题

  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)