PHP Error Handling error_reporting() Function
The PHP Error Handling error_reporting() function is used to specify which errors should be displayed or logged during code execution. It displays exact error messages to help developers find and resolve errors.
You can choose whether to show all mistakes, none, or only certain types of difficulties. This is useful for debugging during development and keeping the site secure in production. Using error_reporting() makes your PHP program more dependable and manageable.
Syntax
Below is the syntax of the PHP Error Handling error_reporting() function −
int error_reporting ( ?int $error_level = null )
Parameters
This function accepts $error_level parameter which specifies the error report level for the current script. Value number and constant name are accepted.
Return Value
The error_reporting() function returns the error_reporting level before it is set to error_level.
Report levels
Here are the reports level listed in the below table −
| Value | Constant | Description | PHP |
|---|---|---|---|
| 1 | E_ERROR | Fatal run-time errors. Errors that cannot be recovered from. Execution of the script is halted | |
| 2 | E_WARNING | Non-fatal run-time errors. Execution of the script is not halted | |
| 4 | E_PARSE | Compile-time parse errors. Parse errors should only be generated by the parser | |
| 8 | E_NOTICE | Run-time notices. The script found something that might be an error, but could also happen when running a script normally | |
| 16 | E_CORE_ERROR | Fatal errors at PHP startup. This is like an E_ERROR in the PHP core | 4 |
| 32 | E_CORE_WARNING | Non-fatal errors at PHP startup. This is like an E_WARNING in the PHP core | 4 |
| 64 | E_COMPILE_ERROR | Fatal compile-time errors. This is like an E_ERROR generated by the Zend Scripting Engine | 4 |
| 128 | E_COMPILE_WARNING | Non-fatal compile-time errors. This is like an E_WARNING generated by the Zend Scripting Engine | 4 |
| 256 | E_USER_ERROR | Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error() | 4 |
| 512 | E_USER_WARNING | Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error() | 4 |
| 1024 | E_USER_NOTICE | User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error() | 4 |
| 2048 | E_STRICT | Run-time notices. PHP suggest changes to your code to help interoperability and compatibility of the code | 5 |
| 4096 | E_RECOVERABLE_ERROR | Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler()) | 5 |
| 8191 | E_ALL | All errors and warnings, except of level E_STRICT | 5 |
PHP Version
First introduced in core PHP 4, the error_reporting() function continues to function easily in PHP 5, PHP 7, and PHP 8.
Example 1
Here is the basic example of the PHP Error Handling error_reporting() function to display all PHP errors and the constant E_ALL. This helps with debugging during the development process.
<?php // Enable reporting of all errors error_reporting(E_ALL); // Undefined variable (will show an error) echo $undefinedVariable; ?>
Output
Here is the outcome of the following code −
Warning: Undefined variable $undefinedVariable in /Users/abc/Desktop/PhpProjects/index.php on line 12
Example 2
This program shows how to block all errors by setting error_reporting() function to zero. This is useful when you do not want error messages to appear on a live website.
<?php // Disable all error reporting error_reporting(0); // Undefined variable echo "No error will be displayed"; echo $undefinedVariable; ?>
Output
This will generate the below output −
No error will be displayed
Example 3
This program demonstrates how to dynamically alter the error reporting level using the error_reporting() method during script execution. It starts with all errors allowed before moving to no error reporting.
<?php // Enable all errors error_reporting(E_ALL); echo "All errors are currently displayed.\n"; // Undefined variable error displayed echo $undefinedVariable; // Turn off error reporting error_reporting(0); echo "Error reporting is now turned off.\n"; // Another undefined variable (no error displayed) echo $anotherUndefinedVariable; ?>
Output
This will create the below output −
All errors are currently displayed. Notice: Undefined variable: undefinedVariable in /path/to/your/script.php on line X Error reporting is now turned off.