duan1933 2010-03-19 04:48
浏览 153
已采纳

PHP简写语法

I've just came across this on GitHub.

 ($config === NULL) and $config = Kohana::config('email');

Is that the equivalent of

if ($config === NULL) {
    $config = Kohana::config('email');
}

Is this commonplace? Would I expect other developers looking at my code if I used that first way to instantly know what it was doing?

  • 写回答

2条回答 默认 最新

  • dr2898 2010-03-19 05:04
    关注

    Took me a second to get it, but that should actually work in just about every programming language. Because the "and" or "or" operators are lazily evaluated, if the statement on the left is false, then there's no need to evaluate the rest of the statements because the whole expression will always be false (false and true is false). Likewise, you can do it with "or", but the statement on the left would have to be true, then the one on the right wouldn't be evaluated.

    PS: It doesn't really matter in this case that what's on the right isn't really a boolean expression; it'll just take on the truth value of $config

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

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大