PHP 7 Error Handling

PHP 7 has a set of predefined error reporting constants -
Name Error Code Description
E_ERROR 1 This is the fatal error occurs at script runtime.
E_WARNING 2 This is the non fatal error that occurs at runtime.
E_PARSE 4 Error that occurs at compile 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, it is 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.




Error Reporting Settings

We can set error reporting settings globally in 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, first line reports all errors except notices, second line displays the error messages as HTML to standard output, third line prevents to log errors to disk and fourth line prevents to track errors and storing the errors in $php_errormsg variable.


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 donot specify an error type, the default is E_USER_NOTICE.

Example -

trigger_error('Error Message', E_USER_ERROR);

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);




error_get_last() function

The error_reporting() function returns the last occurred error.


error_get_last();

error_log() function

It sends the specified error message to a log file or mailing address.


error_log("Error Message", 1, "This email address is being protected from spambots. You need JavaScript enabled to view it.");

set_error_handler() function

It 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);

restore_error_handler() function

It 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();