【发布时间】:2011-02-11 21:41:04
【问题描述】:
我正在尝试使用三元运算符来缩短我的代码。
这是我的原始代码:
if ($type = "recent") {
$OrderType = "sid DESC";
} elseif ($type = "pop") {
$OrderType = "counter DESC";
} else {
$OrderType = "RAND()";
}
如何在我的代码中使用三元运算符而不是 ifs/elses?
$OrderType = ($type = "recent") ? "sid DESC" : "counter DESC" ;
这是我尝试过的代码,但不知道如何添加“elseif 部分”。
【问题讨论】:
-
你不需要改变你的代码,它的可读性很好。
标签: php ternary-operator