如何设置此案例取决于规则

时间:2011-08-15 12:32:23

标签: php

如果我有以下

$x = "go";
$y = "went";
$z "gone";

我需要设置标题所在的标题

$title = $x;              //only if "y" empty no care about "z" if empty or not
$title = $x . " - " . $y; // if "y" not empty and "z" is empty
$title = $y . " - " . $z; // if "z" not empty and "y" not empty

这是我的尝试但场地和/或看起来如此透彻(我是noob)

$empty = ""; // i've creaty empty example
$x = "go";
$y = "went";
$z "gone";

if ($y==$empty) {
$title = $x; // output should be go

} else if($y!=$empty && $z==$empty) {
$title = $x . " - " . $y;  // output should be go - went

} else if($y!=$empty && $z!=$empty) {
$title = $y . " - " . $z;  // output should be went - gone

} else {
$title = $x;  // output should be go
}

有没有人有更好的建议!非常感谢

1 个答案:

答案 0 :(得分:1)

$title = empty($y) ? $x : (empty($z) ? $x."-".$y : $y."-".$z);
相关问题