三元运算符中的多个条件安全吗?
我看到建议说三元运算符不能嵌套。
我已经测试了下面的代码,它工作正常。我的问题是,我以前没有见过像这样使用的三元运算符。那么,这是否像在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果从三元运算符返回的结果只有“true”和“false”,那么您甚至不需要该运算符。你可以这样:
但是,为了回答你的问题,是的,多种条件都可以很好地发挥作用。
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:
But, to answer your question, yes multiple conditions work perfectly well.
它是完全合法的,它有效并且“好像可靠”,但它看起来很难看。
如果将每个三元语句放在括号内,嵌套也可以:
手册中唯一建议反对的是不带括号的嵌套,如下所示:
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:
The only thing that is advised against in the manual is nesting without parenthesis like this:
您是否有理由希望将条件保存到变量中?这是上面的简化版本。
Is there a reason you want to have your conditions saved into a variable? this is the simplified version of above.
如果要返回
true
或false
,则不需要三元。 引用手册:这意味着
已经分配 true 或 false。另外,如果不关心 $rule 是布尔值,则不需要与
==
进行比较。您也不需要大括号,例如与您的初始三元相同。
当您有多个这样的表达式时,一个好的做法是将实际比较隐藏在有意义的方法或函数名称后面,例如
,然后您可以这样做
当然,
conditionsMet
不是that有意义的。更好的示例是isSummerTime
或isEligibleForDiscount
等。只需在方法名称中表达规则所表达的内容即可。您可能还对重构 - 改进现有代码的设计。
You do not need the ternary if you are going to return
true
orfalse
. Quoting the manual:This means
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.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.
and then you can do
Of course,
conditionsMet
isnt that meaningful. A better example would be something likeisSummerTime
orisEligibleForDiscount
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.
你也可以做
You can also do
这是合法的,不一定是“丑陋的”。我经常使用“hook”运算符,在表格形式中它非常干净,例如:
恕我直言,如果用 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.:
This function would, IMHO, be less clear and certainly longer if written with if-else logic.