本文介绍了PHP 速记语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遇到了这个 在 GitHub 上.

I've just came across this on GitHub.

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

这相当于

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?

推荐答案

我花了一秒钟才弄明白,但这实际上应该适用于几乎所有编程语言.因为与"或或"运算符是惰性求值的,如果左边的语句为假,则不需要对其余的语句求值,因为整个表达式将始终为假(假和真为假).同样,你可以用或"来做,但左边的语句必须是真的,然后右边的语句不会被评估.

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: 在这种情况下,右边的不是真正的布尔表达式并不重要;它只会接受 $config

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

这篇关于PHP 速记语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:10