3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php // Stick this with your helper functions function buildMenu($navigation, $currentPage){ echo '<ul>'; foreach ($navigation as $key => $val) { $class = $val === $currentPage ? 'on' : 'off'; echo '<li>'; if( is_array($val) ){ echo '<a href="#" class="' . $class . '">' . $key . '</a>'; buildMenu($val, $currentPage); } else { echo '<a href="' . $val . '" class="' . $class . '">' . $key . '</a>'; } echo '</li>'; }; echo '</ul>'; }; // Define current page $currentPage = 'club.php'; // Build up your navigation arrays $navigation = array( 'News' => '#', 'About' => array( 'Club' => 'club.php', 'Coaches' => 'coaches.php', 'Officials' => 'officials.php', 'Management' => 'management.php' ) ); // Build HTML buildMenu($navigation, $currentPage);
Output for 4.3.0 - 7.1.0
<ul><li><a href="#" class="off">News</a></li><li><a href="#" class="off">About</a><ul><li><a href="club.php" class="on">Club</a></li><li><a href="coaches.php" class="off">Coaches</a></li><li><a href="officials.php" class="off">Officials</a></li><li><a href="management.php" class="off">Management</a></li></ul></li></ul>