Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我刚刚在 GitHub 上看到了这个。
($config === NULL) and $config = Kohana::config('email');
是不是相当于
if ($config === NULL) { $config = Kohana::config('email'); }
这是家常便饭吗?如果我使用第一种方式立即知道它在做什么,我会期望其他开发人员查看我的代码吗?
AND是 PHP 逻辑运算符。
具有相同的结果(但具有较小的运算符优先级)到
($config === NULL) && $config = Kohana::config('email');
就个人而言,为避免任何混淆,我将使用您的第二种方法。
我花了一秒钟才搞定,但这实际上应该适用于几乎所有编程语言。因为“and”或“or”运算符是惰性求值的,如果左边的语句为假,则无需对其余语句求值,因为整个表达式将始终为假(假且真为假)。同样,你可以用“或”来做,但是左边的陈述必须是真的,那么右边的那个就不会被评估。
PS:在这种情况下,右边的不是真正的布尔表达式并不重要。它只会取真值$config
$config