How can include() be utilized to display a custom error page in PHP when a file is not found?
When a file is not found in PHP, you can utilize the include() function to display a custom error page instead of the default server error message. By using a conditional statement to check if the file exists before including it, you can redirect to a custom error page if the file is not found. This helps provide a better user experience by showing a more informative error message.
<?php
$file = 'file_to_include.php';
if (file_exists($file)) {
include($file);
} else {
include('error_page.php');
}
?>
Keywords
Related Questions
- What is the best practice for redirecting to another PHP page if a PHP variable is empty?
- In what situations should the get_message() function be called in a PHP bot script to ensure smooth message processing without blocking the script?
- What potential issues can arise when using a function to clean up HTML code in PHP?