← Back to PHP Course | Chapter 13: Error Handling | Lesson 1 of 5

PHP Error Handling Introduction

Error handling is about what a program does when something goes wrong, like a fire drill planned in advance. PHP has different levels of problems, from small notices to big stopping errors.
Syntax
php
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
<?php
echo $undefinedVar ?? "notice: undefined variable";
echo "\n";
echo 10 / 2; // no error
?>

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
<?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";
?>

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
<?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();
?>

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
<?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;
?>

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
<?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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Showing errors on a live site with display_errors on, which reveals file paths and internal details to visitors.
  2. Turning error reporting off during development, so real bugs are hidden.
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.