PHP Elseif 三元运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13162172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:52:12  来源:igfitidea点击:

PHP Elseif Ternary Operators

php

提问by Jon

I am trying to convert the following code into a Ternary Operator, but it is not working and I am unsure why. I think my problem is that I do not know how to express the elseifoperation in ternary format. From my understanding and elseifis performed the same way as an ifoperation by using the format : (condition) ? 'result'.

我正在尝试将以下代码转换为三元运算符,但它不起作用,我不确定为什么。我想我的问题是我不知道如何elseif用三元格式来表达操作。根据我的理解,elseif它的执行方式与if使用 format的操作相同: (condition) ? 'result'

if ($i == 0) {
    $top = '<div class="active item">';
} elseif ($i % 5 == 0) {
    $top = '<div class="item">';
} else {
    $top = '';
}

$top = ($i == 0) ? '<div class="active item">' : ($i % 5 == 0) ? '<div class="item">' : '';

回答by Samuel Cook

$top = ($i == 0) ? '<div class="active item">' : (($i % 5 == 0) ? '<div class="item">' : '');

you need to add parenthesis' around the entire else block

您需要在整个 else 块周围添加括号

回答by losthorse

The Ternary Operator doesn't support a true if... else if... else...operation; however, you can simulate the behavior by using the following technique

三元运算符不支持真正的if... else if... else...操作;但是,您可以使用以下技术模拟行为

var name = (variable === 1) ? 'foo' : ((variable === 2) ? 'bar' : 'baz');


I personally don't care for this as I don't find it more readable or elegant. I typically prefer the switchstatement.

我个人并不关心这个,因为我不觉得它更具可读性或优雅性。我通常更喜欢这种switch说法。

switch (variable) {
    case 1 : name = 'foo'; break;
    case 2 : name = 'bar'; break;
    default : name = 'bas'; break;
}

回答by Bhaskar Pramanik

Too late probably to share some views, but nevertheless :)

分享一些观点可能为时已晚,但仍然:)

  1. Use if - else if - else for a limited number of evaluations. Personally I prefer to use if - else if - else when number of comparisons are less than 5.
  2. Use switch-case where number of evaluations are more. Personally I prefer switch-case where cases are more than 5.
  3. Use ternary where a single comparison is under consideration (or a single comparison when looping), or when a if-else compare is needed inside the "case" clause of a switch structure.
  4. Using ternary is faster when comparing while looping over a very large data set.
  1. 使用 if - else if - else 进行有限数量的评估。我个人更喜欢在比较次数小于 5 时使用 if - else if - else。
  2. 在评估次数较多的情况下使用 switch-case。就个人而言,我更喜欢 case 超过 5 的 switch-case。
  3. 在考虑单个比较(或循环时的单个比较)时,或者在 switch 结构的“case”子句中需要 if-else 比较时,使用三元。
  4. 在循环非常大的数据集时进行比较时,使用三元会更快。

IMHO Its finally the developer who decides the trade off equation between code readability and performance and that in turn decides what out of, ternary vs. if else-if else vs. switch-case, can be used in any particular situation.

恕我直言,最终由开发人员决定代码可读性和性能之间的权衡等式,然后决定可以在任何特定情况下使用三元与 if else-if else 与 switch-case 中的什么。