Are there best practices for marking both top-level and sub-level menu items as active in PHP navigation?

When building a navigation menu in PHP, it is important to mark both the top-level and sub-level menu items as active based on the current page being viewed. This can be achieved by using conditional statements to check if the current page matches the link of each menu item, and adding a CSS class to indicate the active state.

<nav>
    <ul>
        <li <?php if($currentPage == 'home') echo 'class="active"'; ?>><a href="index.php">Home</a></li>
        <li <?php if($currentPage == 'about') echo 'class="active"'; ?>><a href="about.php">About</a></li>
        <li <?php if($currentPage == 'services') echo 'class="active"'; ?>>
            <a href="services.php">Services</a>
            <ul>
                <li <?php if($currentPage == 'service1') echo 'class="active"'; ?>><a href="service1.php">Service 1</a></li>
                <li <?php if($currentPage == 'service2') echo 'class="active"'; ?>><a href="service2.php">Service 2</a></li>
            </ul>
        </li>
        <li <?php if($currentPage == 'contact') echo 'class="active"'; ?>><a href="contact.php">Contact</a></li>
    </ul>
</nav>