PHP error reporting controls which PHP errors, warnings, and notices are shown or logged, and you configure it with the error_reporting() function, the display_errors and log_errors directives, or the E_ALL constant. In short, you set a level (like E_ALL) to choose which errors to report, and then decide whether to display them on screen or write them to a log file. This article explains how it works, how to configure it per environment, and how to debug common issues.
Formula
The calculator above helps you visualize the relationship between the three main settings. The effective reporting level is simply the value you set for error_reporting() (or the error_reporting ini directive). Whether errors are displayed on screen is the product of the display_errors setting (1 or 0) and whether the reporting level is non-zero. Similarly, whether errors are logged is the product of log_errors and the reporting level. In real PHP code, you would set these with:
error_reporting(E_ALL); // or a specific level like E_ERROR & ~E_WARNING
ini_set('display_errors', '1'); // 1 = show on screen, 0 = hide
ini_set('log_errors', '1'); // 1 = write to error log, 0 = don't
The error_reporting() function accepts an integer bitmask that represents which error levels to report. The most common constants are:
E_ALL– all errors and warnings (value 32767 in PHP 5.4+; in older versions it was less, but now it includes everything).E_ERROR– fatal runtime errors (value 1).E_WARNING– non-fatal runtime warnings (value 2).E_PARSE– compile-time parse errors (value 4).E_NOTICE– runtime notices (value 8).E_STRICT– suggestions to ensure forward compatibility (value 2048, but included in E_ALL since PHP 5.4).E_DEPRECATED– warnings about code that will not work in future versions (value 8192).
You can combine them with bitwise operators: E_ALL & ~E_NOTICE means “report everything except notices.�
Worked Example
Using the default values from the calculator: error_reporting = 32767 (E_ALL), display_errors = 1, log_errors = 1.
- Effective reporting level = 32767 (all error types are reported).
- Display on screen = 1 × (32767 > 0) = 1, so errors will appear in the browser output.
- Log to file = 1 × (32767 > 0) = 1, so errors will also be written to the server error log.
If you set error_reporting to 0, then both display and log become 0, meaning no errors are reported at all. If you set display_errors to 0 but keep log_errors at 1, errors are hidden from the user but still recorded in the log – the recommended setup for production.
How PHP Error Reporting Works
PHP has a built-in error system that categorizes problems into levels. When a script runs, PHP checks each error against the current error_reporting level. If the error’s level is included in the bitmask, PHP takes action based on the display_errors and log_errors directives. The action can be:
- Display the error message in the output (if
display_errorsis on). - Log the error to the server’s error log (if
log_errorsis on). - Both, or neither.
The error_reporting level can be set in three places, with different scopes:
- php.ini – the global configuration file for all PHP scripts on the server. The directive is
error_reporting = E_ALL. - .htaccess (Apache) or nginx config – per-directory or per-server settings. For example,
php_value error_reporting 32767in .htaccess. - At runtime – using
error_reporting()orini_set()within a script. This overrides the php.ini setting for that script only.
It’s important to know that display_errors and log_errors are separate from error_reporting. You can report all errors but choose not to display them, or display only some. The three settings work together.
Choosing the Right Error Reporting Level
The level you choose depends on the environment:
- Development (local): Use
E_ALLanddisplay_errors = 1. You want to see every notice and warning to fix them early. - Production (live): Use
E_ALLfor logging, but setdisplay_errors = 0. You want to know about all issues, but you don’t want users to see error messages that may reveal server paths or database details. Instead, log them to a file. - Legacy code: Sometimes you have old code that generates many notices. You might temporarily set
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED)to hide non-critical messages while you work on other things, but it’s better to fix the code.
A common mistake is setting display_errors = 1 on a live server. This can leak sensitive information and create a poor user experience. Another mistake is using error_reporting(0) to “hide� errors – this also prevents logging, so you lose all trace of problems.
Common Problems and Misconceptions
One frequent issue is that display_errors has no effect when the script has a parse error. Parse errors occur before the script runs, so any runtime ini_set() calls are not executed. To display parse errors, you must set display_errors = 1 in php.ini or .htaccess, not in the script itself.
Another misconception is that error_reporting(E_ALL) will show all errors, but if display_errors is off, you won’t see them. They will be logged only if log_errors is on. So “reporting� does not mean “displaying� – it means “collecting.�
Also, the @ operator (error suppression) temporarily sets error_reporting to 0 for the expression that follows. This can hide errors that you actually want to know about, and it’s slower than proper error handling.
How to Set Error Reporting in Different Environments
Here are concrete examples for common setups:
- php.ini (global):
error_reporting = E_ALLdisplay_errors = On(development) orOff(production)log_errors = On - .htaccess (Apache):
php_value error_reporting 32767php_flag display_errors onphp_flag log_errors on - Nginx + PHP-FPM: You set these in the
php.inior viafastcgi_param PHP_VALUEin the server block. - At runtime (in a script):
error_reporting(E_ALL);ini_set('display_errors', '1');
For a quick test, create a PHP file with <?php echo $undefined_variable; ?> and run it with different settings to see the effect.
Alternatives and Related Functions
Besides error_reporting(), PHP offers other tools for handling errors:
ini_set()andini_get()– to change and read directives at runtime.set_error_handler()– to define a custom function that handles errors instead of the default.error_log()– to send an error message to the log manually.trigger_error()– to generate a user-level error (e.g.,E_USER_NOTICE).display_errorsvslog_errors– the two output channels.
For modern PHP (7+), most errors are exceptions, and you can use try/catch blocks. However, the classic error reporting system is still used for warnings and notices.
Frequently Asked Questions
Why do I see “Notice: Undefined variable� even though I set error_reporting(E_ALL)?
That’s exactly what E_ALL includes – notices. If you don’t want to see notices, you can exclude them with error_reporting(E_ALL & ~E_NOTICE). But it’s better to fix the code by initializing variables.
How do I hide errors from users but still log them?
Set display_errors = 0 and log_errors = 1 in your production environment. You can do this in php.ini, .htaccess, or at the top of your script with ini_set('display_errors', '0'); and ini_set('log_errors', '1');.
What is the difference between error_reporting and display_errors?
error_reporting determines which error levels are considered at all. display_errors determines whether those considered errors are printed to the output. You can have error_reporting(E_ALL) but display_errors off – then errors are collected but not shown, only logged if log_errors is on.
Why does changing error_reporting in my script not work for parse errors?
Parse errors happen before the script is executed, so any code inside the script (including ini_set) is never run. You must set display_errors and error_reporting in the server configuration (php.ini or .htaccess) to see parse errors.
Can I use error_reporting(0) to suppress all errors?
Yes, but it also stops logging, which is rarely desirable. It’s better to use a custom error handler or the @ operator for specific expressions. Suppressing all errors makes debugging very difficult.
What is E_ALL in PHP 8?
In PHP 8, E_ALL includes all error levels, including E_STRICT (which was deprecated) and E_DEPRECATED. The numeric value is still 32767. It’s the recommended setting for development.
Related
Formula used
in err | Error level (E_ALL=32767) | - | 32767 | 0 | 32767 in disp | Display errors (1=on, 0=off) | - | 1 | 0 | 1 in log | Log errors (1=on, 0=off) | - | 1 | 0 | 1 out effective | Effective reporting level | - | err | 0 out show | Errors shown on screen | - | disp * (err > 0) | 0 out write | Errors written to log | - | log * (err > 0) | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Mac Start Up Automatically After Power Failure?
- Expose Self-Hosted Service Securely Without Cloudflare Tunnel
- Nano Delivery for Functional Foods: What It Can and Can’t Do4.00
- Disable Apple Watch Turn-by-Turn Vibration While Driving
- Convert Play FLAC in iTunes: Full Guide
- Soda Foundation Charter: What It Means