html属性之间的条件语句(php)


Conditional Statements(php) between html attributes?

我正在重构一个php网页,我需要动态插入一个类。

根据项目的结构,一种解决方案是使用三元运算符<?($selectedElement=="KeyOne"? 'linkactive': 'normalLink') ?>来处理之前赋值的变量的值:

<a id="customid_generatebytool" class="<?($selectedElement=="KeyOne"? 'linkactive': 'normalLink') ?>" href="anotherPage.php" ...>
....
</a>
<a id="customid_generatebytool" class="<?($selectedElement=="KeyTwo"? 'linkactive': 'normalLink') ?>" href="Page2.php" ...>
....
</a>

但是我上面的代码不工作,我是php的新手。

添加echo

<a id="customid_generatebytool" class="<?php echo ($selectedElement=="KeyOne"? 'linkactive': 'normalLink') ?>" href="anotherPage.php" ...>

您已经使用了php短标记<?