Can you use PHP to dynamically assign the "active" class to a navigation menu item based on the current page?

To dynamically assign the "active" class to a navigation menu item based on the current page in PHP, you can use the $_SERVER['REQUEST_URI'] variable to get the current page URL and compare it with the URL of each menu item. If they match, you can echo out the "active" class for that specific menu item.

<?php
$current_page = $_SERVER['REQUEST_URI'];

$menu_items = array(
    'home' => '/',
    'about' => '/about',
    'services' => '/services',
    'contact' => '/contact'
);

foreach ($menu_items as $key => $value) {
    $class = ($current_page == $value) ? 'active' : '';
    echo '<li class="' . $class . '"><a href="' . $value . '">' . ucfirst($key) . '</a></li>';
}
?>