How can PHP be used to assign a CSS class to a link when the page is accessed?

To assign a CSS class to a link when the page is accessed, you can use PHP to dynamically generate the HTML output with the desired CSS class. This can be achieved by checking a condition, such as the current page URL, and then including the CSS class in the link's HTML output based on that condition.

<?php
$current_page = $_SERVER['REQUEST_URI'];
$class = ($current_page == '/page1.php') ? 'active' : '';

echo '<a href="page1.php" class="' . $class . '">Link 1</a>';
?>