我对一个人手不足的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)
}
}
发布于 2012-09-20 20:38:18
尝尝这个!
($product == "vindo") ? ($number != 14 ? $this->getNextVindoInList($id) : null ) : (($number != 22) ? $this->getNextGandrupInList($id) : null)
发布于 2012-09-20 20:52:00
我不会给出一个带有嵌套三元运算符的解决方案。为什么?带有显式if/else结构的代码传达意图。它显示了到底发生了什么。
为什么要牺牲几行代码的可读性呢?这是一笔相当糟糕的交易。
发布于 2012-09-20 20:47:01
使用下面的代码可以简化if语句:
if($product == "vindo" && $number != 14) {
$this->getNextVindoInList($id)
} else if($number != 22) {
$this->getNextGandrupInList($id)
}
排序函数if现在不方便了,因为else也有一个if语句。
https://stackoverflow.com/questions/12512652
复制相似问题