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.

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

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?