What are some best practices for restructuring code in PHP templates to achieve a horizontal menu layout dynamically?

To achieve a horizontal menu layout dynamically in PHP templates, it's best to use a loop to iterate over the menu items and generate the necessary HTML markup for each item. This allows for easy customization and maintenance of the menu structure without hardcoding each menu item individually.

<nav>
    <ul>
        <?php
        $menuItems = array("Home", "About", "Services", "Contact");
        foreach ($menuItems as $menuItem) {
            echo "<li><a href='#'>$menuItem</a></li>";
        }
        ?>
    </ul>
</nav>