What is the best practice for dynamically updating a menu on multiple pages using PHP?
When dynamically updating a menu on multiple pages using PHP, it is best to create a separate PHP file that contains the menu items. This file can be included on each page where the menu needs to be displayed using PHP's include or require function. By updating the menu items in the separate file, the changes will be reflected on all pages that include it, making it easy to maintain consistency across the website.
// menu.php
$menuItems = [
'Home' => 'index.php',
'About' => 'about.php',
'Services' => 'services.php',
'Contact' => 'contact.php'
];
// menu.php can be included on each page like this
include 'menu.php';
// Display the menu items on the page
echo '<ul>';
foreach ($menuItems as $label => $url) {
echo '<li><a href="' . $url . '">' . $label . '</a></li>';
}
echo '</ul>';