What is the significance of using $_GET[] in PHP for navigation highlighting?
When building a website with multiple pages, it is common to have a navigation menu that highlights the current page the user is on. One way to achieve this in PHP is by using the $_GET[] superglobal array to retrieve the current page's identifier from the URL parameters. By checking if the current page identifier matches the one in the navigation menu, you can apply a specific styling to highlight the active link.
<?php
$current_page = isset($_GET['page']) ? $_GET['page'] : 'home';
function isPageActive($page) {
global $current_page;
if ($current_page == $page) {
echo 'class="active"';
}
}
?>
<!-- Navigation Menu -->
<ul>
<li><a href="index.php?page=home" <?php isPageActive('home'); ?>>Home</a></li>
<li><a href="index.php?page=about" <?php isPageActive('about'); ?>>About</a></li>
<li><a href="index.php?page=services" <?php isPageActive('services'); ?>>Services</a></li>
<li><a href="index.php?page=contact" <?php isPageActive('contact'); ?>>Contact</a></li>
</ul>
Keywords
Related Questions
- How can server performance impact the loading time of PHP scripts, especially when handling multiple requests?
- What are some resources for learning more about PHP control structures and conditional statements?
- Are there any best practices for using regular expressions in PHP to avoid issues with HTML tags?