如何在php中使用三元运算符if-elseif-else?

How to use ternary operators if-elseif-else in php?

我想对 if-elseif-else 使用三元运算符。但这对我不起作用。

请检查以下代码:

$openMon = "00:01";  // The below Line Returns Time
$openMon = "00:00";  // The below Line Returns Close

<td><?=(($openMon == '00:00')?'Close':date("g:i a", strtotime($openMon)));?></td>

以上行完美运行,因为它 returns 对或错。

但是当 $openMon 空白时 returns 凌晨 1 点。我想当 $openMon 空白时 returns "-"

我想要一行三件事,例如:

$openMon = "";        // This is I want in ternary operator with above two conditions

if($openMon == "" ){
    $openMon ="-";
}

我试过了:

<td><?=isset((openMon == "")?'-':(($openMon == '00:00')?'Close':date("g:i a", strtotime($openMon)));?></td>

这对我不起作用。

基本上我想要三元运算符:

if($openMon == ''){
    $openMon = "-";
}else{
    if($openMon == '00:00'){
        $openMon = "Close";
    }else{
        $openMon = date("g:i a", strtotime($openMon)));
    }
}

任何想法或建议都会有所帮助。

您可以使用嵌套的三元运算符。 同样的问题是 here

您的代码将是这样的:

$openMon = !$openMon ? "-" : ($openMon == '00:00' ? "Close" : date("g:i a", strtotime($openMon)))

您可以测试此代码片段 here