建议使用此条件语句的更好方法

时间:2015-07-02 09:19:06

标签: php if-statement conditional

有没有更好的方法来检查这段代码,

if (is_string($test) && $test != NULL) {
    $test = 'yes';
} else {
    $test = null;
}

 if (is_null($test) || !isset($this->_status[$test])) {
    $icon = null;
} else {
    $icon = $this->getImage($this->_status[$test]);
}

2 个答案:

答案 0 :(得分:1)

通过减少代码行,你的意思更好吗?如何使用三元运算符。

$test = is_string($test) ? "Yes" : NULL

答案 1 :(得分:0)

您可以使用速记if / else使用Ternary Operator

{{1}}
相关问题