我有一个简短的 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/