PHP Error Handling Introduction
In this page:
error_reporting(E_ALL);
trigger_error("message", E_USER_WARNING); // or E_USER_NOTICE, E_USER_ERROR
Types of Errors in PHP
PHP distinguishes several error severities: notices flag minor issues like an undefined variable, warnings report bigger problems without halting execution, and fatal errors stop the script immediately -- knowing which is which shapes how urgently you need to react.
Example: Types of Errors in PHP
<?php
echo $undefinedVar ?? "notice: undefined variable";
echo "\n";
echo 10 / 2; // no error
?>
Login to try C/C++/Java/PHP code in the editor
Configuring Error Reporting Level
The error_reporting() function controls which of those severities actually get displayed or logged, letting you show everything during development while silencing minor notices in production.
Example: Configuring Error Reporting Level
<?php
// Call `error_reporting(E_ALL)`
error_reporting(E_ALL);
// Print "All errors, warnings, and notices will now be shown" to the output
echo "All errors, warnings, and notices will now be shown";
?>
Login to try C/C++/Java/PHP code in the editor
Triggering Custom Errors
trigger_error() lets your own code raise a warning or notice on demand, which is useful for flagging suspicious application-level conditions (like a deprecated function call) the same way PHP flags its own issues.
Example: Triggering Custom Errors
<?php
// Define the function `useOldFunction` with no parameters
function useOldFunction() {
// Call `trigger_error("This function is deprecated", E_USER_WARNING)`
trigger_error("This function is deprecated", E_USER_WARNING);
}
// Call `useOldFunction()`
useOldFunction();
?>
Login to try C/C++/Java/PHP code in the editor
Custom Error Handlers
set_error_handler() lets you register a custom function that intercepts PHP's built-in errors, so you can log them, format them consistently, or hide raw error details from end users.
Example: Custom Error Handlers
<?php
set_error_handler(function ($errno, $errstr) {
// Print "Custom handler caught: $errstr" to the output
echo "Custom handler caught: $errstr";
});
// Print `$undefinedVar` to the output
echo $undefinedVar;
?>
Login to try C/C++/Java/PHP code in the editor
Clean Error Recovery
restore_error_handler() reverts back to PHP's default error handling once your custom handler is no longer needed, which matters if you only want custom handling active for a specific block of code.
Example: Clean Error Recovery
<?php
set_error_handler(function ($errno, $errstr) {
// Print "Custom: $errstr\n" to the output
echo "Custom: $errstr\n";
});
// Call `restore_error_handler()`
restore_error_handler();
// Print "Back to PHP's default error handling" to the output
echo "Back to PHP's default error handling";
?>
Login to try C/C++/Java/PHP code in the editor
- Showing errors on a live site with
display_errorson, which reveals file paths and internal details to visitors. - Turning error reporting off during development, so real bugs are hidden.
- Ignoring warnings and notices, which often point to bugs that later become fatal errors.
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: