Sometimes in development we see fatal errors and warnings. Fatal error halts the execution of the page. So in this tutorial I will show you how to handle fatal error in PHP.
In the below example I forced to display Fatal Error:
require 'another-file.php';
If the file ‘another-file.php‘ is not exists you will see fatal error and warnings like below.
Warning: require(another-file.php) [function.require]: failed to open stream: No such file or directory in E:\xampp\htdocs\demo\handle-fatal-error.php on line 3
Fatal error: require() [function.require]: Failed opening required ‘another-file.php’ (include_path=’.;D:\xampp\php\PEAR’) in E:\xampp\htdocs\demo\handle-fatal-error.php on line 3
So I have used error_reporting(0) to disable all type of errors. Also I have used register_shutdown_function() method to handle the fatal error and display our own custom message.
error_reporting(0); function FatalErrorHandling() { $error = error_get_last(); echo $error['message']; } register_shutdown_function('FatalErrorHandling'); require 'another-file.php';
I hope it will help you.