多个布尔条件 - 运算符优先级 [英] Multiple boolean conditions - operator precedence

查看:42
本文介绍了多个布尔条件 - 运算符优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了如下所示的代码行:

I encountered a code line that looks like this:

if ($users == 'all' || $_POST['user'] == 1 && $users == 'admins' || $_POST[ 'user' ] == 0 && $users == 'mods') ...

我不明白如何满足所有这些条件,因为它们之间没有括号:(

I don't understand how are all these conditions met because there are not parentheses between them :(

||&& 更重要吗?首先评估哪些部分?

Is || more important than && ? Which parts get evaluated first?

推荐答案

&& 取决于左表达式为真时右表达式的求值,|| 没有.您可以将其重写为:

&& depends of the evaluation of the right expression when left one is true, || doesn't. You could rewrite it to:

if(
    $users == 'all' ||
    ($_POST['user'] == 1 && $users == 'admins') ||
    ($_POST['user'] == 0 && $users == 'mods')
)

它会是一样的.

这篇关于多个布尔条件 - 运算符优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆