Are there best practices for managing multiple active links in a navigation menu using PHP and CSS?
When managing multiple active links in a navigation menu using PHP and CSS, it's important to dynamically add an "active" class to the current page's link to visually indicate which page is currently active. This can be achieved by checking the current page URL against each menu item's URL and adding the "active" class accordingly.
<?php
$current_page = basename($_SERVER['REQUEST_URI']);
$menu_items = array(
'Home' => 'index.php',
'About' => 'about.php',
'Services' => 'services.php',
'Contact' => 'contact.php'
);
foreach ($menu_items as $label => $url) {
$active_class = ($current_page == $url) ? 'active' : '';
echo '<li class="' . $active_class . '"><a href="' . $url . '">' . $label . '</a></li>';
}
?>