Error handling in PHP is how your script reports, logs, and reacts to problems such as warnings, notices, deprecations, and fatal errors. PHP ships a small set of functions that let you choose which levels to report, raise your own errors, run custom handler code, and inspect what went wrong.
This page explains the ideas. The Error Functions index lists all 14 function guides.
Report in development, log in production.
Use error_reporting(E_ALL) while you build. In production, keep log_errors on and display_errors off so visitors never see file paths or stack details.
Errors are not exceptions.
Warnings and notices go through set_error_handler(). Objects you throw go through try and catch. See PHP Exception for that side, or convert errors into ErrorException inside your handler.
Syntax
PHP Syntax
The usual report, raise, handle, and inspect pattern:
PHP
error_reporting(E_ALL); // choose which levels to report
trigger_error("Low disk space", E_USER_WARNING); // raise a user error
user_error("Same thing", E_USER_NOTICE); // alias of trigger_error()
set_error_handler(function ($errno, $errstr, $file, $line) {
error_log("[$errno] $errstr in $file:$line"); // write to the log
return true; // true = handled
});
restore_error_handler(); // go back to the previous handler
$last = error_get_last(); // array or null
error_clear_last(); // reset it
$frames = debug_backtrace(); // call stack as array
set_exception_handler(function (Throwable $e) {
echo "Uncaught: ", $e->getMessage();
});
Reference
Core Ideas
What to remember when you work with errors in PHP:
Error handling is how your script reports, logs, and reacts to problems such as warnings, notices, and fatal errors. PHP gives you functions to choose which levels to show, raise your own errors, run custom handler code, and read the last error that happened.
Classic PHP errors (warnings, notices, deprecations) are reported through the error system and handled with set_error_handler(). Exceptions are objects you throw and catch with try and catch. A handler can convert an error into an ErrorException if you want one style of handling.
Call error_reporting(E_ALL) to report every level while developing. In production, keep logging on and display off. error_reporting() only chooses which levels PHP cares about; display_errors and log_errors control where messages go.
None. user_error() is an alias of trigger_error(). Both raise a user-level error such as E_USER_WARNING or E_USER_NOTICE, which your set_error_handler() callback can catch.
Use it right after an operation that may fail quietly, especially when you suppress the message with the @ operator. It returns an array with type, message, file, and line, or null if no error happened. Call error_clear_last() to reset it.
Practice the examples here, then open the Error Functions index for all 14 function guides, from error_reporting() and set_error_handler() to debug_backtrace() and set_exception_handler().