PHP Error Handling Functions and Examples

This article explains PHP error handling by describing common error types, introducing built‑in functions such as die(), error_get_last(), error_reporting(), set_error_handler(), and error_log(), and providing code examples that demonstrate how to capture, display, customize, and log errors in backend applications.

php Courses
php Courses
php Courses
PHP Error Handling Functions and Examples

Error handling is an inevitable part of programming; whether developing a small website or a large application, proper handling of errors is required. PHP provides a set of error handling functions to help developers identify and resolve issues.

Error Types

PHP has various error types. Common types include Fatal Error, Parse Error, Warning, and Notice, each representing a different error level and handling approach.

Error handling functions

PHP provides built‑in error handling functions that developers can use to customize their own error handling mechanisms.

die() function

The die() function outputs an error message and terminates script execution. It can be used to stop the script when a severe error occurs and display the corresponding message.

<?php
    $file = fopen("test.txt", "r");

    if (! $file) {
        die("Unable to open file");
    }
    // continue file operations
?>

In this example we attempt to open a non‑existent file; the die() function outputs the error message and stops script execution.

error_get_last() function

The error_get_last() function retrieves the last error that occurred, allowing access to its details for appropriate handling.

<?php
    $file = fopen("test.txt", "r");

    if (! $file) {
        $error = error_get_last();
        echo "Error type: " . $error["type"] . "<br>";
        echo "Error message: " . $error["message"] . "<br>";
        echo "Error file: " . $error["file"] . "<br>";
        echo "Error line: " . $error["line"] . "<br>";
    }
?>

This example also tries to open a missing file, then uses error_get_last() to fetch and display the error details.

error_reporting() function

The error_reporting() function controls which error levels PHP displays. By setting different levels, various error messages can be viewed.

<?php
    error_reporting(E_ALL);

    echo $undefined_variable;
?>

Here we set error_reporting(E_ALL) to show all error levels, then attempt to output an undefined variable, causing PHP to display the notice.

set_error_handler() function

The set_error_handler() function sets a custom error handling function, enabling specific processing when errors occur.

<?php
    function error_handler($errno, $errstr, $errfile, $errline) {
        echo "Custom error handler<br>";
        echo "Error type: " . $errno . "<br>";
        echo "Error message: " . $errstr . "<br>";
    }

    set_error_handler("error_handler");

    echo $undefined_variable;
?>

This example defines a custom error handler and registers it with set_error_handler(); when an error occurs, the custom function outputs the error information.

Error Logs

In addition to the above functions, PHP can write error information to a log file for later analysis using error_log().

<?php
    $file = fopen("test.txt", "r");

    if (! $file) {
        $error = error_get_last();
        error_log("Error type: " . $error["type"] . ", error message: " . $error["message"], 3, "error.log");
    }
?>

In this example we again attempt to open a missing file, retrieve the last error with error_get_last(), and write the details to error.log.

By mastering PHP's error handling functions, developers can promptly detect and manage various errors, improving code reliability and stability; appropriate methods should be chosen based on project requirements.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPError Handling
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.