What are some best practices for extending a dynamic menu in PHP?

When extending a dynamic menu in PHP, it is important to use a modular approach to easily add new menu items without modifying existing code. One way to achieve this is by using an array to store menu items and dynamically generating the menu based on the array contents. By separating the menu logic from the presentation, it becomes easier to maintain and update the menu structure.

<?php
// Define an array of menu items
$menuItems = array(
    'Home' => 'index.php',
    'About' => 'about.php',
    'Services' => 'services.php',
    'Contact' => 'contact.php'
);

// Generate the menu
echo '<ul>';
foreach ($menuItems as $label => $link) {
    echo '<li><a href="' . $link . '">' . $label . '</a></li>';
}
echo '</ul>';
?>