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>';
?>
Keywords
Related Questions
- How can PHP developers differentiate between form submissions from different sources to customize email delivery?
- In the context of the forum thread, what suggestions were given to the user experiencing the issue of "Undefined variable: id"?
- What advice is given by other users in the forum thread to improve the PHP script?