我目前正在将其用于导航列表以突出显示活动页面:
<?= ($current_page == 'thema_list' ? 'class="on"' : '') ?>
扩展此项以检查另一页将是:
<?= ($current_page == 'thema_list' || $current_page == 'thema_edit' ? 'class="on"' : '') ?>
有没有办法避免这种重复?
答案 0 :(得分:3)
对于2个值,这可能是PHP处理它所花费的时间最快的方法,但是如果你有很多值,你可以使用in_array:
<?= (in_array($current_page, array('thema_list', 'thema_edit')) ? 'class="on"' : '') ?>
同时查看this question关于与in_array相关的一些速度测试与仅单独检查每个值的速度测试。
答案 1 :(得分:0)
您可以使用in_array()
$matches = array("thema_list", "thema_edit", "thema_other");
if (in_array($current_page, $matches)) {
echo 'class="on"'
}
答案 2 :(得分:0)
你可以把它们放在一个数组中然后使用:
if(in_array($current_page, $array_values))
{
echo 'class on';
}