What is the best practice for assigning cases to links in a switch statement for including pages in PHP?
When using a switch statement to include different PHP pages based on a case, it is best practice to assign each case to a specific page link. This makes the code easier to read and maintain, as each case clearly indicates which page will be included. Additionally, it helps prevent errors and ensures that the correct page is included for each case.
<?php
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
switch ($page) {
case 'home':
include 'home.php';
break;
case 'about':
include 'about.php';
break;
case 'contact':
include 'contact.php';
break;
default:
include '404.php';
break;
}
?>
Related Questions
- What are potential reasons for receiving an empty output when trying to access source code using cURL?
- How can the system() function in PHP cause the browser to hang when executing certain commands?
- Are there any potential pitfalls or vulnerabilities to be aware of when using encryption functions in PHP for sensitive information?