What are some best practices for handling navigation elements in PHP to indicate the user's current location?

When building a website with multiple pages or sections, it's important to provide users with clear navigation elements to indicate their current location. One common way to do this is by highlighting the active link in the navigation menu based on the current page the user is on. This can be achieved by adding a CSS class to the active link or changing its styling to differentiate it from the other links.

<?php
// Get the current page URL
$current_page = basename($_SERVER['REQUEST_URI']);

// Define an array of navigation links and their corresponding URLs
$navigation_links = array(
    'Home' => 'index.php',
    'About' => 'about.php',
    'Services' => 'services.php',
    'Contact' => 'contact.php'
);

// Loop through the navigation links and add a CSS class to the active link
echo '<ul>';
foreach ($navigation_links as $link_text => $link_url) {
    $class = ($current_page == $link_url) ? 'active' : '';
    echo '<li><a href="' . $link_url . '" class="' . $class . '">' . $link_text . '</a></li>';
}
echo '</ul>';
?>