PHP error reporting and the PHP error log: where the errors go
PHP decides three things about an error: whether to report it, whether to show it, and where to log it. Print those settings first, then read the file.

On this page
Put this at the top of the script that fails, reload once, and read the last two lines of what it prints:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('log_errors', '1');
echo 'sapi: ', PHP_SAPI, "\n";
echo 'ini file: ', php_ini_loaded_file() ?: 'none', "\n";
echo 'error_log: ', ini_get('error_log') ?: '(not set: the web server or stderr gets it)', "\n";On the command line without any php.ini the answer is this. Through a web server you get other values, and those are the ones that count:
sapi: cli
ini file: none
error_log: (not set: the web server or stderr gets it)If the page stays blank with those lines in place, the script has a syntax error. PHP then never runs the first line, so it can't switch anything on. The manual says the same about display_errors set with ini_set(): it has no effect when the script has fatal errors. Check the syntax from a shell with php -l file.php, or make the settings in php.ini, .user.ini or the FPM pool, where they exist before the script is read.
Three switches that people mix up
| Setting | Question it answers | PHP's own default |
|---|---|---|
error_reporting | Which kinds of error count at all? | E_ALL since PHP 8.0 |
display_errors | Are they written into the page? | On |
log_errors | Are they written to a log? | Off |
error_log | Which log? | Not set |
Those are the defaults built into PHP. The php.ini-production file that ships with PHP changes several of them, and your distribution's file is based on it, so don't assume: print.
error_reporting is the filter in front of the other two. An error that the filter drops is neither shown nor logged. display_errors and log_errors are independent, and the pair you want depends on the machine:
; development
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
; production
error_reporting = E_ALL
display_errors = Off
display_startup_errors = Off
log_errors = On
error_log = /var/log/php/app.logNote that error_reporting is the same in both. Turning the filter down in production hides the messages from you and not just from the visitors.
Where the log is
Three cases cover nearly all setups.
error_log names a file. Then that file is it, as long as the user that PHP runs as may write to it. A path that can't be written fails quietly: PHP falls back to the next place. Create the folder and give it to the pool user before you look any further.
error_log is empty and PHP runs behind a web server. The message goes to the server's error stream. With PHP-FPM and nginx it appears in nginx's error.log, in a line that contains FastCGI sent in stderr: "PHP message: .... With Apache's module it appears in Apache's error log.
error_log is empty on the command line. The message goes to stderr, with a time stamp in front when it's logged. That's why a cron job without a redirect mails you its warnings, if the machine can send mail.
PHP-FPM adds one more file, its own log, set by the global error_log in php-fpm.conf. It holds messages about processes: starts, stops, children that died, pools that are full. Script errors from your code land there only if the pool sets catch_workers_output, which the FPM manual gives as off by default.
To see which file a given process writes to, send a message on purpose and search for it:
<?php
ini_set('error_log', __DIR__ . '/app.log');
error_log('marker-4711 from ' . PHP_SAPI);
echo str_contains(file_get_contents(__DIR__ . '/app.log'), 'marker-4711 from cli') ? "found\n" : "not found\n";foundIn the real case leave out the ini_set() line, call the page, and grep for the marker in the candidates. It settles the question in a minute, where guessing can take an hour.
What the levels mean
Each kind of error has a number, and error_reporting is the sum of the kinds you want. The list of constants has them all. These are the ones you meet:
<?php
foreach (['E_ERROR', 'E_WARNING', 'E_PARSE', 'E_NOTICE', 'E_DEPRECATED', 'E_USER_ERROR', 'E_USER_WARNING', 'E_USER_DEPRECATED', 'E_ALL'] as $name) {
printf("%-18s %6d\n", $name, constant($name));
}
echo 'without deprecations: ', E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED, "\n";E_ERROR 1
E_WARNING 2
E_PARSE 4
E_NOTICE 8
E_DEPRECATED 8192
E_USER_ERROR 256
E_USER_WARNING 512
E_USER_DEPRECATED 16384
E_ALL 32767
without deprecations: 8191That run was on PHP 8.2. Write the names in php.ini and in code, not the sums: E_ALL has had different values in different versions. In httpd.conf or an .htaccess file the names have no meaning, because PHP isn't the program that parses them, so the number is needed there.
A warning means that PHP carried on with a replacement value, often false or null. The visible failure comes later, in another function. This is the pattern to learn:
<?php
set_error_handler(function (int $level, string $text) {
echo $level === E_WARNING ? 'warning' : 'other', ': ', $text, "\n";
return true;
});
$lines = file('/no/such/file.txt');
var_dump($lines);
$row = ['name' => 'Ana'];
var_dump($row['email']);warning: file(/no/such/file.txt): Failed to open stream: No such file or directory
bool(false)
warning: Undefined array key "email"
NULLCode that loops over $lines two screens further down then fails with a message about foreach, and that message is the one people search for. Read a log from the first line of a request, not from the last.
Errors, exceptions and the last resort
PHP has two systems that live side by side. Old functions raise errors, which are the levels above. Newer code throws exceptions, and since PHP 7 most fatal conditions in the engine are thrown too, as objects of class Error. Both Error and Exception implement Throwable:
<?php
try {
null_function();
} catch (Throwable $t) {
echo get_class($t), ': ', $t->getMessage(), "\n";
}
try {
strlen([]);
} catch (TypeError $t) {
echo $t->getMessage(), "\n";
}Error: Call to undefined function null_function()
strlen(): Argument #1 ($string) must be of type string, array givenThree functions let an application take over:
<?php
set_error_handler(function (int $level, string $text, string $file, int $line) {
if (!(error_reporting() & $level)) {
return false; // filtered or silenced with @
}
throw new ErrorException($text, 0, $level, $file, $line);
});
set_exception_handler(function (Throwable $t) {
error_log((string) $t); // the full text goes to the log
if (PHP_SAPI !== 'cli') {
http_response_code(500);
}
echo "Something failed. The details are in the log.\n";
});
register_shutdown_function(function () {
$last = error_get_last();
if ($last && in_array($last['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR], true)) {
error_log('fatal: ' . $last['message']);
}
});
ini_set('error_log', __DIR__ . '/app.log');
try {
echo 10 / (int) file_get_contents('/no/such/number.txt');
} catch (ErrorException $e) {
echo 'caught: ', strtok($e->getMessage(), ':'), "\n";
}caught: file_get_contents(/no/such/number.txt)The error handler turns warnings into exceptions, so a failed read stops the calculation and can be caught where it happened. The manual page lists the kinds it never sees, E_ERROR, E_PARSE and the core and compile kinds among them, and that's what the shutdown function is for: running out of memory, for example, can't be caught at all. Returning false from the handler hands the error to PHP's own.
One warning for the handler above: turning deprecations into exceptions makes an update of PHP break pages that still worked. Many projects let E_DEPRECATED and E_USER_DEPRECATED through to the log and throw for the rest.
The @ operator
@ in front of an expression silences it. Since PHP 8.0 it no longer silences fatal errors, and the manual describes what a handler sees: error_reporting() returns a fixed set of fatal kinds and not zero. An old handler that tests error_reporting() === 0 therefore treats every silenced call as a real error. The test with & in the block above is the one that works in all versions.
<?php
set_error_handler(function ($level) {
echo error_reporting(), ' ', error_reporting() & $level ? 'report' : 'ignore', "\n";
return true;
});
@file('/no/such/file.txt');
file('/no/such/file.txt');4437 ignore
32767 report@ belongs in front of calls whose failure you check in the next line, such as @fsockopen(). In front of a whole function of your own it hides every mistake in it.
From a log to a list of work
A production log repeats the same few messages thousands of times. Count them before reading any of them. The sample here has five lines, and the pipeline works the same on a file of any size:
cat > app.log <<'LOG'
[25-Nov-2026 08:00:01 UTC] PHP Warning: Undefined array key "email" in /srv/shop/cart.php on line 41
[25-Nov-2026 08:00:03 UTC] PHP Warning: Undefined array key "email" in /srv/shop/cart.php on line 41
[25-Nov-2026 08:01:10 UTC] PHP Fatal error: Uncaught TypeError: strlen(): Argument #1 ($string) must be of type string, array given in /srv/shop/search.php:12
[25-Nov-2026 08:02:44 UTC] PHP Warning: Undefined array key "email" in /srv/shop/cart.php on line 41
[25-Nov-2026 08:03:00 UTC] PHP Deprecated: Creation of dynamic property Cart::$sum is deprecated in /srv/shop/cart.php on line 9
LOG
sed -E 's/^\[[^]]*\] //' app.log | cut -c1-60 | sort | uniq -c | sort -rn | awk '{$1=$1; print}'3 PHP Warning: Undefined array key "email" in /srv/shop/cart.
1 PHP Fatal error: Uncaught TypeError: strlen(): Argument #1
1 PHP Deprecated: Creation of dynamic property Cart::$sum isThe sed part drops the time stamp, so that equal messages become equal lines. Fix from the bottom up in severity: fatal errors first, because a visitor saw each one, then the warnings with the highest count. Deprecations can wait until you plan the next PHP version, but they're the cheapest preview of what will break there.
The stamp names its time zone, UTC in this sample, which is PHP's default when date.timezone isn't set. Compare the zones before you match a line with the web server's log: the two can differ by hours and still describe the same request.
A message that appears only at certain hours is usually a cron job or a backup. Cut the hour out of the stamp in the same way and count that: cut -c14-15 on the lines above gives the hour alone.
Why nothing is logged
Work down this list when a page fails and the log stays empty:
- Another process, another file. The terminal's
phpand the one behind the site read different configuration. Finding the php.ini in use shows how to ask the right one. - The pool overrides it.
php_admin_value[error_log]orphp_admin_flag[log_errors]in the FPM pool beatsphp.ini, and a script can't change it back. - The folder isn't writable. Ask the process who it is, then test as that user:
sudo -u www-data touch /var/log/php/app.log. - The code changes it. Search for
error_reporting(,ini_set('display_errors'andini_set('log_errors'. WordPress, for one, has debugging constants of its own, and withWP_DEBUGon andWP_DEBUG_LOGset to true it writes towp-content/debug.log. - The framework caught it. Laravel, Symfony and others log to their own files.
- PHP never ran. A 502 or 504 is the web server's message about PHP. Its cause is in the web server's log, and the 502 page goes through the lines.
- The disk is full.
df -handdf -i. The second one counts inodes, and a folder with millions of session files can use them up.
A restart belongs after every change in php.ini or a pool file. Reload the FPM service, because it's the process that read the file.
Keep secrets out
A displayed error shows paths, SQL and sometimes a password, since a stack trace can contain the arguments of every function in it. With zend.exception_ignore_args = On PHP leaves arguments out of traces. PHP 8.2 adds an attribute for single parameters:
<?php
function connect(string $user, #[SensitiveParameter] string $password): void
{
throw new RuntimeException('no route to host');
}
try {
connect('shop', 'hunter2');
} catch (RuntimeException $e) {
echo preg_replace('~^#0 .*?: ~', '', strtok($e->getTraceAsString(), "\n")), "\n";
}connect('shop', Object(SensitiveParameterValue))The log itself deserves the same care. Put it outside the web root, readable by the people who need it, and rotate it. An upload that exceeds its limit or a database that went away can write thousands of lines a minute, and a log nobody rotates is how the full disk in item 7 comes about.
Same subject
nginx 502 Bad Gateway with PHP-FPM: read the error log line first
A 502 means nginx got no usable answer from PHP-FPM. One line in the nginx error log says why: no socket, no permission, a dead worker or a big header.
Where is php.ini? Finding the file PHP actually loads
Run php --ini for the command line, and call php_ini_loaded_file() through the browser for the website. They are often two different files, and both count.
MySQL server has gone away (2006) and lost connection (2013): the causes
MySQL error 2006 means the connection was closed before your query arrived: check idle timeout, packet size and server restarts, in that order, then reconnect.
PHPSolvedThe fix first, then the reason