dongqin1861 2014-07-16 15:44
浏览 49
已采纳

使用具有三元条件的串联运算符?

Is it possible to run two ternary conditionals, concatenating the results of the second to the first?

My conditionals read as follows:

$tr_class  = (!$pinned ?: "warning");
$tr_class .= ($read ?: " unread");

echo "<tr class='" . $tr_class . "'>";

If something is $pinned but !$read, I should get:

<tr class='warning unread'>

But I only get the ' unread'? It seems like the second ternary conditional is overwriting the former, and not concatenating?

Is there away around this, without writing full if/else statements?

  • 写回答

1条回答 默认 最新

  • dongtou9934 2014-07-16 15:51
    关注

    Yes is the short answer. If you could not concatenate the outputs of variables / invocation return values in PHP it would not have come very far as a language.

    Your code works. I suspect the problem is with the values of $pinned and $read before it gets to your conditional statements.

    If you neglect to put the 'is true' side of the ternary, it will take the value of the test which will usually be true or false, you should only leave this out if you want the 'is true' value to be the same as the conditional statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料