PHP error types: E_ERROR, E_WARNING, E_NOTICE, E_DEPRECATED
Beginner5 min read·php-10-001
interview
Concept
PHP has two distinct error reporting systems: the traditional error system (used by the engine and some extensions) and the exception system (used by user code and increasingly by the engine). Understanding both is essential for building robust applications.
Error levels: PHP errors have numeric levels combined with the E_* constants:
E_ERROR(1): Fatal errors — script stops immediately. Cannot be caught withtry/catch(butset_error_handlerwon't see them;register_shutdown_functioncan).E_WARNING(2): Non-fatal warnings — execution continues.file_get_contentson a missing file emitsE_WARNING.E_NOTICE(8): Minor issues — using undefined variables, string-to-array index access. Often indicative of bugs.E_DEPRECATED(8192): Functions or features that will be removed in a future version.E_STRICT(2048): Suggestions for forward-compatibility. Merged intoE_ALLin PHP 7.E_USER_*variants: User-triggered viatrigger_error().
error_reporting(E_ALL): Sets which error levels to report. In development, use E_ALL. In production, suppress E_NOTICE and E_DEPRECATED display (but log them).
ini settings that matter:
display_errors = Offin production (don't leak error details to users).log_errors = Onalways (log errors server-side for debugging).error_log = /var/log/php_errors.logor usestderrfor Docker/Systemd.
Code Example
php
<?php
declare(strict_types=1);
// Error level constants
echo E_ERROR; // 1
echo E_WARNING; // 2
echo E_NOTICE; // 8
echo E_DEPRECATED; // 8192
echo E_ALL; // 32767 (all errors)
// Controlling error reporting
error_reporting(E_ALL); // all errors (development)
error_reporting(E_ALL & ~E_NOTICE); // all except notices
error_reporting(0); // suppress all (never do this in development)
// trigger_error — generate user-level errors
trigger_error("Something unexpected happened", E_USER_WARNING);
trigger_error("Check this usage", E_USER_DEPRECATED);
// E_USER_ERROR would be fatal: trigger_error("Fatal!", E_USER_ERROR);
// Suppressing a specific call with @ (use sparingly — hides legitimate errors)
$result = @file_get_contents('/nonexistent'); // suppresses E_WARNING
// Better: check existence first
if (file_exists('/nonexistent')) {
$result = file_get_contents('/nonexistent');
}
// Error log output
error_log("Something went wrong"); // writes to configured error_log file
error_log("Data: " . json_encode(['key' => 'value']));
// PHP 8.0: many functions that used to emit E_WARNING now throw Exceptions/Errors
// Example: json_decode with JSON_THROW_ON_ERROR
try {
$data = json_decode('{invalid}', true, flags: JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
echo "JSON error: " . $e->getMessage();
}
// checking error reporting level
$level = error_reporting();
if ($level & E_NOTICE) {
echo "Notices are enabled\n";
}