php - 嵌套简写如果

标签 php if-statement shorthand-if

我有一个简短的 if 语句我无法弄清楚的问题

($product == "vindo") ? $this->getNextVindoInList($id) : $this->getNextGandrupInList($id),

这工作正常,但我想在该语句中再次检查。像这样:
if($product == "vindo") {
  if($number != 14) {
    $this->getNextVindoInList($id)
  }
} else {
 if($number != 22) {
    $this->getNextGandrupInList($id)
 }
}

最佳答案

出于教育目的,我将保留此答案不变。但要知道,这是不推荐 .嵌套三元组是一个坏主意。与显式 if-else 语句相比,它没有提供任何性能优势,并使代码更难以阅读。

也就是说,请参阅下面的内容,但是 不应该完成。

两种方式:

($product == "vindo" && $number != 14 ? $this->getNextVindoInList($id) : ($number != 22 ? $this->getNextGandrupInList($id) : '')

// Equivalent of:
if ($product == "vindo" && $number != 14)
    $this->getNextVindoInList($id);
else if ($number != 22)
    $this->getNextGandrupInList($id);

// OR

// Equivalent of your example:
($product == "vindo" ? ($number != 14 ? $this->getNextVindoInList($id) : '') : ($number != 22 ? $this->getNextGandrupInList($id) : ''))

关于php - 嵌套简写如果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12512652/

相关文章:

PHP PhantomJS 忽略 SSL 证书

javascript - 值变量作为 0 不等于简写 if 表达式中的 false

javascript 简写 if else 回调

php - 如何在 Wordpress 分页上向 Prev/Next 添加类? (分页链接)

php - Laravel 搜索查询 - 'OR' 和 'AND' 的 SQL 排序破坏了 foreach 循环查询

php - 使用 %LIKE% 的 Codeigniter 自动更新搜索

python - 如何用我的elif if语句纠正语法错误。我究竟做错了什么?

java - 如何以更优雅和可扩展的方式编写这些条件语句

Python - 如果字典中的值那么

c# - C# 有简写 "something if (condition);"语句吗?