How to Add class='active' to an HTML Menu with PHP
The core issue preventing your navigation from working is a misunderstanding of how PHP variables interact with string concatenation versus template rendering. In your current setup, you are defining $nav as a raw string literal containing HTML and PHP tags. When you wrap the content in <<<EOD, you create an end-of-file (EOF) string. While EOF strings allow multi-line readability, they treat everything between the opening delimiter and the closing EOD; as plain text unless explicitly told otherwise.
However, your specific problem lies in how PHP evaluates that string later. When you execute echo $nav;, PHP outputs the exact characters stored in $nav. It does not re-parse the code inside those quotes. The line <li><a <? if($page == 'one'): ?> class="active"...</li> is treated as static text within the variable, meaning the browser simply sees and renders it as HTML source code rather than executing the PHP logic inside it. To make dynamic conditional logic work, the PHP condition must be evaluated at runtime, not embedded inside a string that gets echoed as-is.
To fix this, you need to separate the static HTML structure from the dynamic data injection. The most robust approach in modern PHP development is to generate the menu items using an array and a loop, or by concatenating the conditional logic directly into the echo statement without wrapping the entire block in a single heredoc string that treats the inner logic as text.
Using an Array-Based Navigation
The cleanest pattern involves defining your navigation data in a PHP array and iterating over it. This allows you to pass context (like the current page) to the menu generator function.
Here is how you can refactor common.php to accept the $page variable as an argument:
<?php
function renderNavigation($currentPage) {
$navItems = [
['href' => 'index.php', 'label' => 'Tab1'],
['href' => 'two.php', 'label' => 'Tab2'],
['href' => 'three.php', 'label' => 'Tab3']
];
$html = '<div id="nav"><ul>';
foreach ($navItems as $item) {
// Determine if this link is the active one
$activeClass = ($currentPage === $item['href']) ? ' class="active"' : '';
$html .= '<li><a' . $activeClass . ' href="' . htmlspecialchars($item['href']) . '">' .
htmlspecialchars($item['label']) . '</a></li>';
}
$html .= '</ul></div>';
return $html;
}
// In index.php, you would call: require_once('common.php'); echo renderNavigation($page);
?>
By moving the logic into a function that returns a string, you ensure that the conditional check happens before the string is ever sent to the browser. You must pass $page from your specific page files (like index.php) into this function so it knows which link to highlight.
Alternative: Direct Concatenation
If you prefer not to use functions or arrays, you can keep the structure simple by concatenating the PHP condition directly into the echo statement. This avoids the EOF string trap entirely.
In your common.php, define the static parts as separate strings and combine them where necessary:
<?php
$nav = '<div id="nav"><ul>';
$nav .= '<li><a href="index.php"' . ($page == 'one' ? ' class="active"' : '') . '>Tab1</a></li>';
$nav .= '<li><a href="two.php">Tab2</a></li>';
$nav .= '<li><a href="three.php">Tab3</a></li>';
$nav .= '</ul></div>';
?>
Here, the ternary operator ($page == 'one' ? ...) is evaluated immediately by the PHP engine before the string is assigned to $nav. This guarantees that when you later echo $nav, the class="active" attribute is already part of the string content.
Why Your Original Code Failed
Your original attempt placed the entire HTML block inside a heredoc (<<<EOD). While valid for storing multi-line strings, it created a context where PHP stopped parsing logic inside quotes until it hit the closing delimiter. Even if you had not wrapped it in quotes, simply echoing a string containing < ?php tags does not execute them; the browser never sees PHP code, only the output. The echo $nav statement outputs the literal characters of the string variable. To get execution, the logic must reside outside the echoed string or be part of a dynamic build process where PHP actively constructs the result before returning it.
If you are building a larger application, consider using a framework like Laravel. Frameworks handle this separation of concerns and active state management out of the box, often via Blade templates which allow mixing PHP logic and HTML syntax more intuitively. For more on modern PHP practices, see LaravelCompany.