What are the advantages of using an array with key-value pairs for mapping URL parameters to file locations in PHP instead of a switch statement?

Using an array with key-value pairs for mapping URL parameters to file locations in PHP is advantageous over using a switch statement because it allows for easier maintenance and scalability. With an array, you can easily add or modify mappings without having to change the structure of the code. Additionally, arrays provide faster lookups compared to switch statements, especially when dealing with a large number of mappings.

// Mapping URL parameters to file locations using an array
$routes = [
    'home' => 'pages/home.php',
    'about' => 'pages/about.php',
    'contact' => 'pages/contact.php'
];

// Get the requested URL parameter
$route = $_GET['route'] ?? 'home';

// Check if the route exists in the array, if not, default to 'home'
$file = $routes[$route] ?? $routes['home'];

// Include the corresponding file
include $file;