Add class="active" to active page using PHP

You can add the "active" class to the active page using PHP by comparing the current URL to the URL of the page you want to mark as active. Here's an example of how you could do this:

<ul>
  <li class="<?php echo basename($_SERVER['PHP_SELF']) == 'index.php' ? 'active' : ''; ?>">
    <a href="index.php">Home</a>
  </li>
  <li class="<?php echo basename($_SERVER['PHP_SELF']) == 'about.php' ? 'active' : ''; ?>">
    <a href="about.php">About</a>
  </li>
  <li class="<?php echo basename($_SERVER['PHP_SELF']) == 'contact.php' ? 'active' : ''; ?>">
    <a href="contact.php">Contact</a>
  </li>
</ul>

Watch a course Learn object oriented PHP

This code uses the basename() function to get the current file name, and compares it to the desired file name using the ternary operator. If the current file name is equal to the desired file name, the "active" class is added to the <li> element, otherwise an empty string is added.