How can a PHP switch statement be used to handle 404 error pages effectively?
When a user requests a page that does not exist on a website, the server typically returns a 404 error page. To handle 404 error pages effectively in PHP, you can use a switch statement to check the requested URL and display a custom error page if the requested page is not found.
<?php
// Get the requested URL
$request_url = $_SERVER['REQUEST_URI'];
// Use a switch statement to handle different cases
switch ($request_url) {
case '/page1':
include('page1.php');
break;
case '/page2':
include('page2.php');
break;
// Add more cases for other pages as needed
default:
// If the requested page is not found, display a custom 404 error page
include('404.php');
break;
}
?>
Related questions
-
strpos return value comparison
-
path separators PHP developers code
-
PHP MySQL Smarty