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.
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.
This parameter specifies the error-report level for the current script.
If the level parameter is not given, it will return the current level. Otherwise, it will revert to the old error_reporting level.
||E_STRICT has become a part of E_ALL.
|E_DEPRECATED and E_USER_DEPRECATED are newly added in PHP 5.3.
||E_RECOVERABLE_ERROR is added in PHP 5.2.
||E_STRICT is newly introduced in PHP 5.0.
With the help of PHP program specify different levels of error reporting:
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.
- 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.
- 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.
- 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.