【发布时间】:2013-04-12 09:51:04
【问题描述】:
我需要在 echo 语句中使用 switch 之类的语句
我找到了一种方法,但我认为这不是最好的方法 当我们想在 echo 中使用 IF 时,写一些类似的东西:
echo ((condition)?'print this if condition is True':'print this if condition is False');
我们可以将这种方法用于 switch 之类的方式:
echo ((case1)?'case 1 result':((case2)?'case2 result':((case3)?'case3 result':'default result')));
你知道更好的方法吗?
【问题讨论】:
-
如何让它成为国家 - 将 echo 放在案例中?
-
第一个示例中的三元运算符是在回显中执行此操作的最佳方法。否则,您需要在 switch 语句中构建 html 字符串并回显结果。
-
我认为您应该使用
switch或一系列if语句。链式三元运算符非常难以阅读。 -
能和你一起思考,你心里有什么实际用途?
标签: php switch-statement echo