How can JavaScript be integrated with PHP to improve the functionality of navigation menus?

To improve the functionality of navigation menus, JavaScript can be integrated with PHP to create dynamic and interactive menus. This can be achieved by using PHP to generate the HTML structure of the menu and then using JavaScript to add interactivity such as dropdowns, animations, or highlighting active menu items.

<?php
// PHP code to generate navigation menu items
$navItems = array(
    "Home" => "index.php",
    "About" => "about.php",
    "Services" => "services.php",
    "Contact" => "contact.php"
);

echo '<ul>';
foreach ($navItems as $label => $url) {
    echo '<li><a href="' . $url . '">' . $label . '</a></li>';
}
echo '</ul>';
?>