PHP error_reporting

The error_reporting() is a pre-defined function of PHP. It allows you to control how many and which PHP errors will be reported. As we already discussed that PHP has several levels of errors. Using the error_reporting() function sets that level for the duration of your current script.

The php.ini file has an error_reporting directive that will be set at runtime by this function.

Syntax

snippet
error_reporting (int $level)

The $level is an optional parameter in error_reporting() function. If the level is not set, this function will return the current error reporting level.

level (Optional)

This parameter specifies the error-report level for the current script.

Return Values

If the level parameter is not given, it will return the current level. Otherwise, it will revert to the old error_reporting level.

Changes

Versions Description
PHP 5.4 E_STRICT has become a part of E_ALL.
PHP 5.3

 

E_DEPRECATED and E_USER_DEPRECATED are newly added in PHP 5.3. 
PHP 5.2 E_RECOVERABLE_ERROR is added in PHP 5.2.
PHP 5.0 E_STRICT is newly introduced in PHP 5.0.

Example

With the help of PHP program specify different levels of error reporting:

snippet
<?
	// Turn off all error reporting
	error_reporting(0);
	
	// Report all PHP errors 
	error_reporting(E_ALL);

	// Report all PHP errors
	error_reporting(-1);
	
	// Report all errors except E_NOTICE	
	error_reporting(E_ALL & ~E_NOTICE);
	
	// Report simple running errors
	error_reporting(E_WARNING | E_ERROR | E_PARSE);

	// E_NOTICE is also good to report uninitialized variables
	error_reporting( E_WARNING |E_ERROR | E_PARSE | E_NOTICE);

	// It is same as error_reporting(E_ALL);
	ini_set('error_reporting', E_LL);	
?>

Important Points of error_reporting()

  • By passing zero (0) in error_reporting function, you can remove all errors, warnings, notices, and parse messages. It would be much better to turn off report messages in .htaccess or in ini file instead of having this code in each or every PHP file.
snippet
error_reporting(0);
  • PHP allows developers to use undeclared variables. But these undeclared variables can cause problems for the application when used in conditions and loop.
    Sometimes, this could happen that the variable which is declared and being used in conditions or loops have different spellings. So, to display the undeclared variable in the web application, pass the E_NOTICE in the error_reporting function.
snippet
error_reporting(E_NOTICE);
  • The error_reporting() function allows the specific error to be displayed, which a user want. Using the ~ character, you can filter the error. For example - ~E_NOTICE means that notices will not be displayed. In the below line of code, all the errors will be displayed except E_NOTICE.
snippet
error_reporting(E_ALL & ~E_NOTICE)
  • Below are the three lines of code given, which works same as the error_reporting(E_ALL) that means it will also display all PHP errors. The error_reporting(E_ALL) is most widely used to display errors because it is easy to read and understand.
snippet
error_reporting(-1);
error_reporting(E_ALL)
ini_set('error_reporting', E_ALL);
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +