【问题标题】:How to shorthand following statement如何简写以下语句
【发布时间】:2020-12-14 02:42:38
【问题描述】:

我正在尝试简写以下 if else 语句。但出现错误。

if ($jobs->due_out == null) {
 return ('N/A');
} else {
 return date(self::DATETIME, strtotime($jobs->due_out));
}

这是我迄今为止尝试过的:

return 'N/A' ? $jobs->due_out == null : date(self::DATETIME, strtotime($jobs->due_out);

这是在 PHP。有什么建议吗?

【问题讨论】:

    标签: php coding-style conditional-operator


    【解决方案1】:

    你的说法有点错误,你需要先条件......

    return ($jobs->due_out == null) ? 'N/A' : date(self::DATETIME, strtotime($jobs->due_out));
    

    【讨论】:

    • 非常感谢。我也试过这个方法。但缺少开始和结束括号。这就是为什么我得到一个错误。非常感谢我的朋友。
    • 你甚至不需要 () 在条件周围。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 2012-04-09
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 2022-06-20
    相关资源
    最近更新 更多