What are some best practices for highlighting the currently selected link in a navigation menu using PHP?
To highlight the currently selected link in a navigation menu using PHP, you can add a conditional statement to check if the current page matches the link in the menu. If they match, you can apply a CSS class to style the selected link differently. This can help users easily identify their current location on the website.
<?php
$current_page = basename($_SERVER['REQUEST_URI']);
$menu_items = array(
'Home' => 'index.php',
'About' => 'about.php',
'Services' => 'services.php',
'Contact' => 'contact.php'
);
foreach ($menu_items as $label => $url) {
$class = ($current_page == $url) ? 'active' : '';
echo '<a href="' . $url . '" class="' . $class . '">' . $label . '</a>';
}
?>
Related Questions
- Is there a recommended approach for ensuring consistent text display across different devices and screen sizes when using PHP for text manipulation?
- What are some recommended resources or tutorials for learning PHP 5 and its best practices?
- What is the recommended approach for storing and retrieving variables (strings) in a session in PHP?