What are some best practices for determining the active URL in PHP for menu customization?

When customizing menus in PHP, it is important to determine the active URL to apply specific styling or functionality. One way to achieve this is by comparing the current URL with the URLs of menu items to determine which one is active. This can be done by using the $_SERVER['REQUEST_URI'] variable to get the current URL and then checking it against the URLs of menu items.

$current_url = $_SERVER['REQUEST_URI'];

$menu_items = array(
    '/home' => 'Home',
    '/about' => 'About',
    '/services' => 'Services',
    '/contact' => 'Contact'
);

foreach ($menu_items as $url => $label) {
    $class = ($current_url == $url) ? 'active' : '';
    echo '<a href="' . $url . '" class="' . $class . '">' . $label . '</a>';
}