What are the best practices for loading new pages in the browser using PHP, especially when handling multiple links and includes?

When loading new pages in the browser using PHP, especially when handling multiple links and includes, it is best to use a routing system to manage the different pages and their corresponding PHP files. This helps keep the code organized and makes it easier to add new pages in the future. Additionally, using includes or requires to pull in common elements such as headers, footers, and navigation menus can help reduce code duplication and make the website more maintainable.

<?php
// index.php

// Define a routing system to map URLs to PHP files
$routes = [
    '/' => 'home.php',
    '/about' => 'about.php',
    '/contact' => 'contact.php'
];

// Get the current URL
$url = $_SERVER['REQUEST_URI'];

// Check if the URL exists in the routing system
if(array_key_exists($url, $routes)) {
    // Include the corresponding PHP file
    include $routes[$url];
} else {
    // Handle 404 error
    include '404.php';
}
?>