PHP Error Types and Reporting
Understand notices, warnings, and fatal errors plus error_reporting levels.
PHP Error Types and Reporting is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
PHP Error Types
PHP has several severity levels of errors:
- Notice — minor issue, execution continues
- Warning — non-fatal issue, execution continues
- Fatal Error — execution stops immediately
- Parse Error — syntax error, stops before execution
- Deprecated — feature will be removed
error_reporting()
Control which errors PHP reports with error_reporting():
<?php
// Show all errors (development)
error_reporting(E_ALL);
// Hide notices and deprecated
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
// Production: no display, log to file
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('error_log', '/var/log/php_errors.log');display_errors vs log_errors
Key php.ini settings for error handling:
display_errors = On— show errors in browser (dev only)display_errors = Off— hide from users (production)log_errors = On— write to error_log fileerror_log = /path/to/file— specify log location
Never display errors to users in production!
Triggering Errors with trigger_error
Generate your own PHP errors with trigger_error():
<?php
function divide(int $a, int $b): float {
if ($b === 0) {
trigger_error('Division by zero', E_USER_WARNING);
return 0.0;
}
return $a / $b;
}
echo divide(10, 0); // triggers E_USER_WARNINGCustom Error Handler
Register your own error handler with set_error_handler():
<?php
set_error_handler(function(int $errno, string $errstr, string $file, int $line): bool {
$message = "[$errno] $errstr in $file:$line";
error_log($message);
if ($errno === E_USER_ERROR) {
http_response_code(500);
die('Internal error — please try again later');
}
return true; // don't execute PHP internal error handler
});
trigger_error('Custom error message', E_USER_ERROR);Common Error Sources
Typical causes of PHP errors:
- Accessing undefined array key → Notice
- Calling a function that returns false, then using the result → Warning
- Calling an undefined function → Fatal Error
- Missing semicolon → Parse Error
- Division by zero → Warning (returns INF or NAN)
Error Suppression Operator @
The @ operator suppresses errors from a single expression — avoid it, use proper error handling:
<?php
// Bad: hides errors silently
$result = @file_get_contents('missing.txt');
// Good: check explicitly
$result = file_get_contents('missing.txt');
if ($result === false) {
// handle the error
echo 'File not found';
}PHP 8 JIT and Error Changes
PHP 8 made some error handling stricter:
- Many warnings promoted to TypeError/ValueError
str_contains/starts_with/ends_withreplacing regex for simple checks- Passing wrong argument types throws TypeError by default
ini_set at Runtime
Override php.ini settings at runtime:
<?php
// Display errors during development
if (getenv('APP_ENV') === 'development') {
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
} else {
ini_set('display_errors', '0');
ini_set('log_errors', '1');
error_reporting(E_ALL);
}Checking Error Level
Detect error levels with bitwise AND:
<?php
$level = error_reporting();
if ($level & E_NOTICE) {
echo 'Notices are reported';
}
if ($level & E_WARNING) {
echo 'Warnings are reported';
}
// Check specific error types
echo (E_ALL & E_NOTICE) ? 'E_ALL includes E_NOTICE' : 'no';Error Logging Best Practices
Good error logging habits:
- Always log errors in production
- Include context: user ID, request URL, timestamp
- Use structured logging (JSON) for easier parsing
- Rotate log files to prevent disk fill
- Monitor logs with tools like ELK, Datadog, or Papertrail
Quick Check
Which PHP error type stops execution immediately?
Recap: PHP Error Reporting
Error handling essentials:
- Error types: Notice, Warning, Fatal, Parse, Deprecated
- Set level with
error_reporting() - Display off + log on in production
- Register custom handler with
set_error_handler() - Never suppress with
@in production code
Frequently asked questions
Is the “PHP Error Types and Reporting” lesson free?
Yes — the full text of “PHP Error Types and Reporting” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “PHP Error Types and Reporting”?
Understand notices, warnings, and fatal errors plus error_reporting levels. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “PHP Error Types and Reporting” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- PHP Error Types and Reporting
- Try, Catch, and Finally
- Creating Custom Exceptions
- Logging Errors and Best Practices