Errors are PHP telling you something is wrong. Learning to read them is one of the most valuable skills in programming.
The main kinds
| Kind | What it means | Does the script continue? |
|---|---|---|
| Parse error | The code has a syntax mistake | No, it never starts |
| Fatal error | Something impossible happened (call to an undefined function, uncaught exception) | No, it stops |
| Warning | Something is wrong, but PHP can go on (missing file in include, undefined variable) | Yes |
| Notice | Something might be wrong (rare in PHP 8) | Yes |
| Deprecated | You use a feature that will be removed | Yes |
What an error message looks like
<?php
echo $missing;
Warning: Undefined variable $missing in /path/to/index.php on line 2
Read it in parts:
- Warning is the level.
Undefined variable $missingis the problem./path/to/index.phpandon line 2is where.
A fatal error
<?php
echo "Before\n";
undefinedFunction();
echo "After";
Before
Fatal error: Uncaught Error: Call to undefined function undefinedFunction() in /path/to/index.php:3
Stack trace:
#0 {main}
thrown in /path/to/index.php on line 3
PHP printed Before, then stopped. After never ran. In PHP 8, most fatal errors are real Error objects that you can catch (see Exceptions).
Deprecated
<?php
function oldStyle($a = 1, $b)
{
return $b;
}
echo oldStyle(1, 2);
Deprecated: Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter in /path/to/index.php on line 2
2
The code still works, but a future PHP version may stop allowing it. Fix deprecations when you see them.
Error levels
PHP has constants for each level: E_ERROR, E_WARNING, E_NOTICE, E_DEPRECATED, E_ALL and more. error_reporting() decides which are reported.
<?php
error_reporting(E_ALL); // report everything: use this while developing
echo error_reporting() === E_ALL ? "All errors reported" : "Some hidden";
All errors reported
Since PHP 8, the default error_reporting is E_ALL.
Where do errors go?
Two separate settings:
display_errors: show errors on the page. Good for development, never for a live site.log_errors: write errors to a log file. Always on in production.
See Production Error Handling.
What changed in PHP 8
Several mistakes that used to be quiet notices are now louder:
- “Undefined variable” and “Undefined array key” are warnings.
- Passing the wrong type to a built-in function throws a TypeError.
- Dividing by zero throws a DivisionByZeroError.