How to display all errors in PHP?
A PHP application generates several levels of errors and warnings during the runtime of the script. PHP provides four different ways to display these errors and warnings, which are listed below:
- error_reporting: It displays all level errors except E-NOTICE, E-STRICT, and E_DEPRECATED level errors.
- display_errors: By default, the value of display_errors is off. Set it to on to display all the errors, including parse (syntax) error.
- log_errors: The default value of log_errors is ON, which indicates that the error logging should be done or not.
- error_log string: The error_log string sets that file name where scripts error should be logged.
There are few lines of code given below, add this to your PHP file to display errors. It is a fastest way to display all the PHP errors and warnings.
The working of above function and directives is as follow:
ini_set()
This function tries to override the configuration that is found in php.ini file.
display_errors
The display_errors is a directive that determines whether the error will display to the user or remain hidden. It does not handle the errors that occur during PHP’s startup sequence.
display_startup_errors
The display_startup_errors is also a directive, which is used to find the error during the startup sequence of PHP.
error_reporting()
The error_reporting is a native function of PHP. It is used to display errors.
Program
Output
The output will be shown a warning to the browser.
Warning: include(jtp.php): failed to open stream: No such file or directory in C:xampphtdocsprogramphperror.php on line 6 Warning: include(): Failed opening 'jtp.php' for inclusion (include_path='C:xamppphpPEAR') in C:xampphtdocsprogramphperror.php on line 6
Note: Both these directives display_errors and display_startup_errors would not be able to display the parse errors. Therefore, the PHP.ini configuration must be modified.
Configure PHP.ini to display all errors and warnings
The following changes must be done in php.ini file to display all errors, including parse error, and restart the apache server in xampp.
Set the display_errors directive to “on” in PHP.ini file. It will display all the errors, which cannot be displayed by just calling ini_set() function, such as – syntax and parse errors
Program
PHP program when display_errors is disabled or set to off in php.ini file.
Output
The output will be shown to the browser like the below screenshot when the display_errors directive is disabled.
Output:
The output for the above program when display_errors is enabled or set to on in php.ini file, and the server is restarted.
Error Report Level
As we already discussed that PHP produces different levels of errors. So, let’s learn what kind of errors is generated in PHP code.
Constant | Description |
---|---|
E_ERROR | Fatal runtime error. The execution of the script has been stopped. |
E_WARNING | Non-fatal runtime error. The execution of the script does not stop. |
E_PARSE | Compile-time error, which is generated by the parser. |
E_NOTICE | It is a runtime notice. The PHP script found something that could be an error. |
E_USER_ERROR | E_USER_ERROR is similar to the E_ERROR, but it is produced by the PHP script using the trigger_error() function. It is a fatal user-generated error message. |
E_USER_WARNING | E_USER_WARNING is a non-fatal user-generated warning, and it is similar to the E_WARNING. But it is also generated by the PHP script using trigger_error() function. |
E_USER_NOTICE | It is a user-generated notice, similar to the E_NOTICE. |
E_STRICT | It is not strictly an error. It became part of E_ALL after PHP version 5.4.0. |
E_ALL | Enables all errors and warnings as well. |