How can the active menu item be highlighted in a PHP list navigation?
To highlight the active menu item in a PHP list navigation, you can add a conditional statement to check if the current page matches the menu item. If it does, you can apply a CSS class to highlight it.
<?php
$current_page = basename($_SERVER['REQUEST_URI']);
$menu_items = array(
'Home' => 'index.php',
'About' => 'about.php',
'Services' => 'services.php',
'Contact' => 'contact.php'
);
echo '<ul>';
foreach ($menu_items as $label => $url) {
$class = ($current_page == $url) ? 'active' : '';
echo '<li><a href="' . $url . '" class="' . $class . '">' . $label . '</a></li>';
}
echo '</ul>';
?>