Or is there an even better way?
Switch + a bunch of echos:
switch($currentpage) {
case 'index.php':
$indexclass=' active';
$otherpageclass='';
break;
case 'otherpage.php':
$indexclass='';
$otherpageclass=' active';
break;
}
Then, inside the li class I would simply echo out the $indexclass for index and $otherpageclass for other page.
The other option would be to just set the $currentpage variable and do something like:
<li class="<?php if($currentpage='index.php'){echo ' active';}?>">whatever</li>
<li class="<?php if($currentpage='otherpage.php'){echo ' active';}?>">whatever</li>
Obviously, my site is much larger than this and will probably have about 30 different menu items so I’m wondering which way is most efficient, or if there’s an even better way.
Thank you in advance!
I think speed should have nothing to do with this decision, as you are talking about micro-optimizations at best.
Do what is best for code readability and maintainability. In my opinion, I would tend to lean more towards the approach where you are using the switch statement as, to me, this gives a cleaner separation between display logic and the actual display.