How can the active tab in PHP be passed as a variable?
To pass the active tab in PHP as a variable, you can use a combination of HTML and PHP to dynamically set the active tab based on the current page. You can achieve this by using a conditional statement to check if the current page matches the tab, and then adding an 'active' class to the tab accordingly.
<?php
$current_page = basename($_SERVER['PHP_SELF']);
?>
<ul class="nav">
<li class="<?php echo ($current_page == 'home.php') ? 'active' : ''; ?>"><a href="home.php">Home</a></li>
<li class="<?php echo ($current_page == 'about.php') ? 'active' : ''; ?>"><a href="about.php">About</a></li>
<li class="<?php echo ($current_page == 'contact.php') ? 'active' : ''; ?>"><a href="contact.php">Contact</a></li>
</ul>
Keywords
Related Questions
- In the context of the forum thread, how can redundant code in the index.php file be optimized for better performance?
- Are there best practices for handling HTML content in forum posts using PHP?
- How can PHP developers ensure that their code is robust enough to handle variations in input data like numeric strings for postal codes?