Are there best practices for using PHP to indicate active links on a webpage?
To indicate active links on a webpage using PHP, you can compare the current URL with the link URL and add a CSS class to highlight the active link. This can be achieved by checking if the current URL matches the link URL and adding a specific class to the active link.
<?php
$current_url = $_SERVER['REQUEST_URI'];
function isActive($url) {
global $current_url;
if ($url === $current_url) {
return 'active';
} else {
return '';
}
}
?>
<ul>
<li><a href="/" class="<?php echo isActive('/'); ?>">Home</a></li>
<li><a href="/about" class="<?php echo isActive('/about'); ?>">About</a></li>
<li><a href="/services" class="<?php echo isActive('/services'); ?>">Services</a></li>
<li><a href="/contact" class="<?php echo isActive('/contact'); ?>">Contact</a></li>
</ul>