Simplify if-else-statement


Simplify if-else-statement

有没有更简单的方法来编写下面的代码?这将比2箱大得多,所以很难很快处理。

if ($result == 'yes')
{
    echo 'this text';
}
else if ($result == 'no')
{
    $result = in_string($kairos_array, explode(' ', $input_string));
    if ($result == 'yes')
    {
        echo 'that text';
    }
}

你可能可以重新组织你的代码-创建一个函数,当你发现你需要的只是使用return。这样嵌套就不会增加,代码也更容易阅读。

if ($result == 'yes')
{
    return 'this text';
}
$result = in_string($kairos_array, explode(' ', $input_string));
if ($result == 'yes')
{
    return 'that text';
}

(注意,我已经删除了$result == 'no'的检查,因为如果值只能为'yes'或'no',则根本不需要它)