PHP Error Handling

Error handling in PHP is simpler. PHP provides a rich set of predefined functions and constants for handling error. Error handling refers to the routines in a program that react to unusual information or conditions. The nature of such schedules depends on the clarity of the error messages and the choices given to users for settling the issue.
Error handling is important because it makes it simpler for the end users of your code to use it correctly. Another important issue is that it makes your code simpler to maintain.



Creating Custom Error Handler

In PHP, it is easy to define a custom error handling function. PHP provides framework for error handling that we can be called when an error occurs.

Syntax -
error_function(error_level,error_message,error_file,error_line,error_context)
Parameter Description
error_level It is required and specifies the error report level for the user-defined error.
error_message It is required and specifies the error message for the user-defined error.
error_file It is an optional and specifies the file name in which the error occurred.
error_line It is an optional and specifies the line number in which the error occurred.
error_context It is an optional and specifies an array containing every variable and their values in use when the error occurred.


Error Report levels

PHP has a set of predefined different types of error reporting constants -

Name Error Code Description
E_ERROR 1 This is the fatal error that occurs at script runtime.
E_WARNING 2 This is the non-fatal error that occurs at runtime.
E_PARSE 4 Error that occurs at compiling time due to invalid syntax.
E_NOTICE 8 Nonfatal "notice." Not exactly an error, but a hint that you may be doing something you don't want to, such as dividing a number by zero.
E_CORE_ERROR 16 Fatal error that occurs when the PHP engine starts. You cannot run any PHP scripts if this error occurs.
E_CORE_WARNING 32 Nonfatal error that occurs when the PHP engine starts. You can still run PHP scripts, but you may have one or more problems depending on the error.
E_COMPILE_ERROR 64 Fatal error that occurs when the script is compiled.
E_COMPILE_WARNING 128 Nonfatal error that occurs when the script is compiled.
E_USER_ERROR 256 User-generated fatal error. Same as E_ERROR, but never thrown by PHP. You can throw this error with the trigger_error() function. If you are using PHP's default error handler, then using this error causes script execution to stop.
E_USER_WARNING 512 User-generated nonfatal error. Same as E_WARNING, but never thrown by PHP. You can throw this error with the trigger_error() function.
E_USER_NOTICE 1024 User-generated notice. Same as E_NOTICE, but never thrown by PHP. You can throw this error with the trigger_error() function.
E_ALL 2047 Not really a type of error. Instead, they are all the errors rolled into one. This makes it easy to say that you want to report all of the errors when using the error_reporting() function.




PHP Error Reporting Settings

We can set error reporting settings globally in the php.ini file. These are the codes to change the error reporting for all your scripts.

error_reporting = E_ALL & E_NOTICE
display_errors = On
log_errors = Off
track_errors = Off

In the above code, the first line reports all errors except notices, the second line displays the error messages as HTML to standard output, the third line prevents to log errors to disk and the fourth line prevents to track errors and storing the errors in $php_errormsg variable.


PHP Triggering Error

The function trigger_error() can be used to trigger your own errors. This function requires an error message and optionally an error type. If you don't specify an error type, the default is E_USER_NOTICE.

Example -

trigger_error('Error Message', E_USER_ERROR);

PHP error_reporting() function

The error_reporting() function allows you to override the default error reporting settings used in 'php.ini'.

The error_reporting() function takes one argument, which can either be a string that lists the names of the error reporting settings that you want to use (separated by bitwise operators), or an integer based error level. For example -

// Turn off all error reporting
error_reporting(10);

// Reporting all errors except E_NOTICE
error_reporting (E_ALL ^ E_NOTICE);




PHP error_get_last() function

In PHP, the error_reporting() function returns the last occurred error.


error_get_last();

PHP error_log() function

In PHP, the error_log() function is used to send the specified error message to a log file or a mailing address.


error_log("Error Message", 1, "testmail@domain.com");

PHP set_error_handler() function

The set_error_handler() function is used to set a user-defined error handler function.


function setErrorHandler($errno, $errline) {
	$msg = "The error no is ".$errno." on line no ".$errline
}
// Set error handler function
set_error_handler(setErrorHandler);

PHP restore_error_handler() function

The restore_error_handler() function is used to restore the previous error handler after changing it through set_error_handler() function.


function setErrorHandler($errno, $errline) {
	$msg = "The error no is ".$errno." on line no ".$errline
}
// Set error handler function
set_error_handler(setErrorHandler);

// Restore previous error handler
restore_error_handler();





Read more articles


General Knowledge



Learn Popular Language