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:

  1. error_reporting: It displays all level errors except E-NOTICE, E-STRICT, and E_DEPRECATED level errors.
  2. display_errors: By default, the value of display_errors is off. Set it to on to display all the errors, including parse (syntax) error.
  3. log_errors: The default value of log_errors is ON, which indicates that the error logging should be done or not.
  4. 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.

snippet
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.

Program

snippet
<?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.

Output
Warning: include(jtp.php): failed to open stream: No such file or directory in C:\xampp\htdocs\program\phperror.php on line 6 Warning: include(): Failed opening 'jtp.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\program\phperror.php on line 6
Note
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.

snippet
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

Program

PHP program when display_errors is disabled or set to off in php.ini file.

snippet
<?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.

How to display all errors in PHP

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.

How to display all errors in PHP

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.
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents