How can PHP be utilized to handle included pages within a website's structure for navigation purposes?
To handle included pages within a website's structure for navigation purposes, you can use PHP to dynamically include different files based on the navigation menu item clicked by the user. This allows for a modular and scalable website structure where common elements like headers, footers, and sidebars can be included on each page without duplicating code.
<?php
// Define an array with navigation menu items and corresponding file paths
$pages = array(
'Home' => 'home.php',
'About' => 'about.php',
'Services' => 'services.php',
'Contact' => 'contact.php'
);
// Check if a page parameter is set in the URL, if not default to 'Home'
$page = isset($_GET['page']) ? $_GET['page'] : 'Home';
// Include the selected page file
if (array_key_exists($page, $pages)) {
include $pages[$page];
} else {
echo 'Page not found';
}
?>