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:
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.
ini_set ('display_errors', 1); ini_set ('display_startup_errors', 1); error_reporting (E_ALL);
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.
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); //include a php file which does not exist include("jtp.php"); ?>
Output
The output will be shown a warning to the browser.
The following changes must be done in php.ini file to display all errors, including parse error, and restart the apache server in xampp.
display_errors = on
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
PHP program when display_errors is disabled or set to off in php.ini file.
<?php //semicolon is missing, which generates parse error in php code for ($i = 5; $i >= 0 $i--) { echo "It will generate parse error"; } ?>
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.
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. |