我正在开发一个PHP项目并收到错误:
代码:
echo'<table>
<td> ' .( ($approve == 1 AND $decline == 0 )? "<span style='color: green'> approved</span>"
: ($approve == 0 AND $decline == 0 )? "<span style='color: orange'>
Pending </span>" : "<span style='color: red'> Declined </span>"
). ' </td> </table>';
使用三元运算符的上述格式是否正确?因为我无法获得正确的价值。请告诉我这是什么错误?
编辑:
错误是我无法获得批准,而是我正在等待和拒绝。
答案 0 :(得分:1)
使用“()”
时出错$approve = 1;
$decline = 0;
$res = ($approve == 1 AND $decline == 0)
? "<span style='color: green'> Approved</span>"
: (
($approve == 0 AND $decline == 0)
? "<span style='color: orange'> Pending </span>"
: "<span style='color: red'> Declined </span>"
);
echo $res;
第一个条件的第三个操作数必须在 括号中。
使用括号的代码:
echo '<td>'
. (($approve == 1 AND $decline == 0)
? "<span style='color: green'> Approved</span>"
: (
($approve == 0 AND $decline == 0)
? "<span style='color: orange'> Pending </span>"
: "<span style='color: red'> Declined </span>"
))
. '</td>';