What are some potential issues with using a switch case for content inclusion in PHP?
One potential issue with using a switch case for content inclusion in PHP is that as the number of cases increases, the code can become cluttered and difficult to maintain. To solve this issue, you can use an array to map case values to file paths and include the corresponding file dynamically.
// Define an array mapping case values to file paths
$pages = [
'home' => 'pages/home.php',
'about' => 'pages/about.php',
'contact' => 'pages/contact.php'
];
// Check if the requested page exists in the array
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
// Include the corresponding file
if (array_key_exists($page, $pages)) {
include $pages[$page];
} else {
include 'pages/error.php';
}