如何将 echo 与 shorthand IF、ELSEIF 和 ELSE 一起使用

How to use echo along with shorthand IF, ELSEIF and ELSE

嗨,

我之前看到有人问过这个问题,但那里的答案不是我要找的。我想知道像这个例子这样的表达式的 shorthand 是什么:

if ($gender == 'male'){
    echo 'M;
} else if($gender == 'female'){
    echo 'F';
} else {
    echo 'undefined';
}

只有两种可能时,我才知道该怎么办:

echo ($gender == 'male' ? 'M' : 'F');

但是当你有 else if 就像我上面的例子一样呢?

谢谢。

如果语句为假并且需要在整个 else 块周围添加括号,请再次检查变量

echo ($gender == 'male') ? 'M' : (($gender == 'female') ? 'F' : 'undefined');