PHP 简写语法
我刚刚遇到这个 GitHub。
($config === NULL) and $config = Kohana::config('email');
这相当于
if ($config === NULL) {
$config = Kohana::config('email');
}
这很常见吗?如果我使用第一种方法立即知道它在做什么,我是否会期望其他开发人员查看我的代码?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AND 是 PHP 逻辑运算符。
具有相同的结果(但具有较低的运算符优先级)就
个人而言,为了避免任何混淆,我会使用您的第二种方法。
AND is a PHP logical operator.
has equivalent outcome (but has a lesser operator precedence) to
Personally, to avoid any confusion I would use your second approach.
我花了一点时间才明白,但这实际上应该适用于几乎所有编程语言。因为“and”或“or”运算符是延迟计算的,所以如果左侧的语句为 false,则无需计算其余语句,因为整个表达式将始终为 false(false 和 true 为 false)。同样,您可以使用“或”来完成此操作,但左侧的语句必须为真,然后右侧的语句将不会被评估。
PS:在这种情况下,右边的内容实际上不是一个布尔表达式并不重要;它只是一个布尔表达式。它只会采用
$config
的真实值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