什么,我通常是在过去所做的,为了避免这种可能出现的问题是:
<?php
// This is merely a convenience method for short circuiting,
// you could use print() as it always returns 1, however e()
// is just shorter.
function e($arg)
{
echo $arg;
return true;
}
$isOverDeadline = true;
$something = "Hello world";
?>
<tr style="<?php $isOverDeadline and e('background-color: #cc3300;'); ?>">
<td style="<?php $isOverDeadline and e('background-color: #ffffff;'); ?>"
width="250"><?php e($something); ?></td>
</tr>
这使用short circuit evaluation到尽量减少内联PHP。正如你所看到的,一些IDE或者语法高手会在它上面发挥作用,但我运行的是NetBeans,看起来不错。
请记住,我的例子会导致空的样式属性时$isOverDeadline
是假的,但你可以根据需要修改(生成代码看起来而不必应付前面加上空格清洁等)
你甚至可以复制else
:
<tr style="background-color: <?php ($isOverDeadline and e('#f00')) or e('#0ff'); ?>"></tr>
这将是真正的红色,错误的青色。
来源
2012-02-17 06:15:05
Dan
什么不行?输出HTML是什么样的?您在样式规则中缺少分号,但不应该破坏标记。 – 2012-02-17 03:28:35
所以根本没有输出,或者php没有解释,或者你知道它到底有什么用处? – s1lence 2012-02-17 03:29:41
作为一个建议,通常最好分配一个CSS类,而不是放入内联样式属性。 'if($ isOverDeadline){echo'class =“over”'; }' – 2012-02-17 03:29:53