Error Handling In PHP

Error handling is the process of catching errors raised by your program and then taking appropriate action. If you handle errors properly it can have many unforeseen consequences.

Need for error handling

We have all been using ATM kiosks for account operations. Imagine you walk into one of the kiosks, swipe your card, provide a PIN and a amount to be withdrawn. You wait for few minutes, but cash is not dispensed, neither there is any message on the ATM screen and your are lost as to what happened!!
Users of your web application may have similar experience if your application does not handle error conditions well. If in the scenario above, there would have been a message on the ATM display "Sorry!! The PIN entered is incorrect", "Sorry!! There is no sufficient balance in your account" would it not be much better?
PHP engine triggers errors when an condition prevents proper execution of script. PHP allows scripts also to trigger errors when a particular condition is identified. PHP provides 15 different error types called error levels. Each of this error level is represented by an error code and error constant.

Error Level

Error Level Value Description
E_ERROR 1 A fatal runtime error
E_WARNING 2 A runtime warning, may not be fatal
E_PARSE 4 A problem while parsing script(syntax errors)
E_NOTICE 8 A possible run-time error, may also be generated by normal PHP script
E_CORE_ERROR 16 A fatal error triggered during PHP engine startup
E_CORE_WARNING 32 A non fatal error triggered during PHP engine startup
E_COMPILE_ERROR 64 A fatal script error triggered during compilation
E_COMPILE_WARNING 128 A non fatal script error triggered during compilation
E_DEPRECATED 8192 Warning about deprecated PHP code

Error handling – php.ini directives

Four important directives in php.ini to configure errors at PHP engine level.

Directive Description
error_reporting Which error levels to be triggered
Default Value: E_ALL & ~E_NOTICE
Development Value: E_ALL | E_STRICT
Production Value: E_ALL & ~E_DEPRECATED
display_errors Whether to display errors on webpage
Default & Development Value: On
Production Value: Off
log_errors Whether to log errors in error log file
Default and Development Value: On
Production Value: Off
error_log Path of error log file

Custom error handling

set_error_handler() can be used to execute custom error handler instead of the PHP default handler. Name of the function is given as argument.
Note : The error can also be explicitly triggered using trigger_error().

#Syntax set_error_handler
#Syntax trigger_error
trigger_error("File does not exist");
trigger_error("File does not exist", E_USER_WARNING)	             

Custom error settings

php.ini error directives can be customized through scripts. The script level settings will only affect current script

error_reporting(E_ALL & ~E_NOTICE)
error_reporting(0); // Turn off all error reporting
error_reporting(-1); // Report all PHP errors
error_log("file not found"); //Logged in default log file
error_log("file not found","new_error.log"); //Logged in new_error.log