三元运算符中的多个条件安全吗?

发布于 2024-12-02 20:01:27 字数 395 浏览 2 评论 0原文

我看到建议说三元运算符不能嵌套。

我已经测试了下面的代码,它工作正常。我的问题是,我以前没有见过像这样使用的三元运算符。那么,这是否像在 if 中使用的那样可靠,或者类似的东西稍后会出现并咬我(不是在术语或可读性方面,而是在失败方面)。

$rule1 = true;
$rule2 = false;
$rule3 = true;

$res = (($rule1 == true) && ($rule2 == false) && ($rule3 == true)) ? true : false;

if($res) {
    echo "good";        
} else {
    echo "fail";
}

谢谢!

I have seen advice that says the ternary operator must not be nested.

I have tested the code below and it works okay. My question is, I haven't seen the ternary operator used like this before. So, is this as reliable as it were used in an if or could something like this come and bite me later(not in terms or readability, but by failing).

$rule1 = true;
$rule2 = false;
$rule3 = true;

$res = (($rule1 == true) && ($rule2 == false) && ($rule3 == true)) ? true : false;

if($res) {
    echo "good";        
} else {
    echo "fail";
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

征棹 2024-12-09 20:01:27

如果从三元运算符返回的结果只有“true”和“false”,那么您甚至不需要该运算符。你可以这样:

$res = (($rule1 === true) && ($rule2 === false) && ($rule3 === true))

但是,为了回答你的问题,是的,多种条件都可以很好地发挥作用。

If the results you are returning from the ternary operator are only "true" and "false", then you don't even need the operator. You can just have:

$res = (($rule1 === true) && ($rule2 === false) && ($rule3 === true))

But, to answer your question, yes multiple conditions work perfectly well.

蓝天白云 2024-12-09 20:01:27

它是完全合法的,它有效并且“好像可靠”,但它看起来很难看。

如果将每个三元语句放在括号内,嵌套也可以:

$res = ( $rule1 ? true : ( $rule2 ? true : false ) )

手册中唯一建议反对的是不带括号的嵌套,如下所示:

$res = ( $rule1 ? true : $rule2 ? true : false )

It's totally legal, it works and is "as reliable as if", but it looks ugly.

If you put each ternary statement inside parenthesis, nesting would be fine too:

$res = ( $rule1 ? true : ( $rule2 ? true : false ) )

The only thing that is advised against in the manual is nesting without parenthesis like this:

$res = ( $rule1 ? true : $rule2 ? true : false )
荭秂 2024-12-09 20:01:27

您是否有理由希望将条件保存到变量中?这是上面的简化版本。

if($rule1 && !$rule2 && $rule3)
{
    echo "good";
}
else
{
    echo "bad";
}

Is there a reason you want to have your conditions saved into a variable? this is the simplified version of above.

if($rule1 && !$rule2 && $rule3)
{
    echo "good";
}
else
{
    echo "bad";
}
不及他 2024-12-09 20:01:27

如果要返回 truefalse,则不需要三元。 引用手册:

表达式(expr1) ? (expr2) : (expr3) 如果 expr1 计算结果为 TRUE,则计算结果为 expr2;如果 expr1 计算结果为 FALSE,则计算结果为 expr3。

这意味着

$res = (($rule1 == true) && ($rule2 == false) && ($rule3 == true));

已经分配 true 或 false。另外,如果不关心 $rule 是布尔值,则不需要与 == 进行比较。您也不需要大括号,例如

$res = $rule1 && !$rule2 && $rule3;

与您的初始三元相同。

当您有多个这样的表达式时,一个好的做法是将实际比较隐藏在有意义的方法或函数名称后面,例如

function conditionsMet($rule1, $rule2, $rule3) {
    return $rule1 && !$rule2 && $rule3;
}

,然后您可以这样做

if (conditionsMet($rule1, $rule2, $rule3)) {
    // do something
}

当然,conditionsMet 不是that有意义的。更好的示例是 isSummerTimeisEligibleForDiscount 等。只需在方法名称中表达规则所表达的内容即可。

您可能还对重构 - 改进现有代码的设计

You do not need the ternary if you are going to return true or false. Quoting the manual:

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

This means

$res = (($rule1 == true) && ($rule2 == false) && ($rule3 == true));

will assign true or false already. Also, if dont care about $rule being booleans, you dont need the comparison with ==. You also dont need the braces, e.g.

$res = $rule1 && !$rule2 && $rule3;

is the same as your initial ternary.

A good practise when you have multiple expressions like that is to hide the actual comparison behind a meaningful method or function name, e.g.

function conditionsMet($rule1, $rule2, $rule3) {
    return $rule1 && !$rule2 && $rule3;
}

and then you can do

if (conditionsMet($rule1, $rule2, $rule3)) {
    // do something
}

Of course, conditionsMet isnt that meaningful. A better example would be something like isSummerTime or isEligibleForDiscount and so on. Just express what the rules express in the method name.

You might also be interested in Simplifying Conditional Expressions from the book Refactoring - Improving the design of existing code.

吻风 2024-12-09 20:01:27

你也可以做

 $res = ($rule1 && !$rule2 && $rule3);

You can also do

 $res = ($rule1 && !$rule2 && $rule3);
苍暮颜 2024-12-09 20:01:27

这是合法的,不一定是“丑陋的”。我经常使用“hook”运算符,在表格形式中它非常干净,例如:

bool haveANeed() 
{ 
    //     Condition       result
    //     ----------      ------
    return needToEat()   ? true
         : needToSleep() ? true
         : needToStudy() ? true
         : needToShop()  ? true
         : needToThink() ? true
         :                 false; // no needs!
}

恕我直言,如果用 if-else 逻辑编写,这个函数会不太清晰,而且肯定会更长。

It's legal and doesn't have to be "ugly". I use the "hook" operator often, in table form it's quite clean, e.g.:

bool haveANeed() 
{ 
    //     Condition       result
    //     ----------      ------
    return needToEat()   ? true
         : needToSleep() ? true
         : needToStudy() ? true
         : needToShop()  ? true
         : needToThink() ? true
         :                 false; // no needs!
}

This function would, IMHO, be less clear and certainly longer if written with if-else logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文