DP Full Stack Passionate Developer



Catch Fatal Errors in PHP

July 29, 2017, by admin, category PHP, PHP Errors

In this instructional exercise we will perceive how to get the FATAL Error in PHP. Ordinarily we can’t reserve FATAL Error on the grounds that those are FATAL.

This will be valuable when you need to demonstrate particular all around arranged blunder page as opposed to PHP Error, when your program closes out of the blue because of some deadly mistakes.

We will utilize register_shutdown_function capacity to get the FATAL Error and configuration as we require.

The main capacity, register_shutdown_function(), enables us to indicate a capacity that will be executed when the PHP content quits running, regardless of whether it be from completing normally due it arriving at the end, or falling flat as a result of a lethal PHP blunder.

Remember that register_shutdown_function will gets executed after your content execution closes possibly it is effective or closes with any blunder.

So before playing out any activity in this capacity we have to check if truly any lethal happens or not. On the off chance that happens then we will demonstrate blunder page else it will keep on running as it seems to be. So to get the points of interest of the last blunder we will utilize error_get_last() work.

The issue is, how would we call register_shutdown_function()? On the off chance that the content you need to screen for blunders has en mistake, similar to a parse blunder, and you put the register_shutdown_function() bring in that content it won’t work, in light of the parse blunder. What’s more, utilizing “incorporate” or “require” won’t work either in light of the fact that that basically is as yet a similar content, the extent that php is concerned.


/** Handling fatal error @return void */
function fatalErrorHandler()
          # Getting last error
          $error = error_get_last();
          # Checking if last error is a fatal error
          if(($error[‘type’] === E_ERROR) || ($error[‘type’] === E_USER_ERROR))
                 # Here we handle the error, displaying HTML, logging, …
                 echo ‘Sorry, a serious error has occured in ‘ . $error[‘file’];
# Registering shutdown function


We would now be able to utilize it with register_shutdown_function to deal with deadly blunder, log, divert, html page, rather than the notorious white page.

So, what do you think ?