PHP Error Handling (With Examples)

By Sayesha Singh Last updated : December 22, 2023

What is Error Handling?

Error handling is an essential part when scripts and web applications are created. Without the error handling code in any program, the program appears to be unprofessional and has a potential security risk.

Error Handling in PHP Programming

To handle errors in PHP is uncomplicated and necessary so that the program knows the appropriate action which should be taken on encountering such errors.

There are three main error checking methods,

  1. die() function
  2. Custom error checking
  3. Reporting errors

PHP Error Handling: The die() Function

This function allows displaying of the user-friendly message when a particular error occurs. This allows for better interaction between the program and the user.

For example, there is a file that is not found by the code. Then it prints an error that is confusing and complicated.

Example

<?php
    // Trying to open a file which is 
    // not present in the system
    $file=fopen("technology.txt","r");
?>

Output

PHP Warning:  fopen(technology.txt): failed to open stream: 
No such file or directory in /home/jdoodle.php on line 4

Hence for better error displaying of the message if the file is not found, a program can be written as below,

<?php
    // Checking for the file name
    if(file_exists("technology.txt")) {
        $file = fopen("technology.txt", "r");
    } else {
        // If the file is not found message will be printed
        die("Error: The mentioned file was not found");
    }
?>

Output

Error: The mentioned file was not found

PHP Error Handling: Custom Error Handling Function

We can check and customize error checking by simply creating a function. It will be called when an error is present.

There is a minimum of two arguments which are needed to be passed in the parameter. One is the error level while other is the message. At maximum, five arguments can be passed at once.

Syntax

function_name(level,message,error_file,line,context)

Parameters

  • function_name: the name of the error function made to handle the error.
  • level: It is a number specifying the level of the error.
  • message: Message which needs to be displayed
  • Error_file(optional): mentions the name of the file in which error happened.
  • Line(optional): mentions the line number in which error happened
  • Context(optional): An array with all the variable in use and their values

Here is the program to make custom error checking when trying to print a variable that is not present.

Example

<?php
    //error handling function
    function catching_Error($no, $str) {
        echo "<b>Error:</b> [$no] $str";
    }
    
    //set error handler
    set_error_handler("catching_Error");
    
    //error trigging function
    echo($uu);
?>

Output

<b>Error:</b> [8] Undefined variable: uu

PHP trigger_error() Function

If the function or any code receives data that is logically not correct then trigger_error() function can be used for displaying a message.

For example, a teacher wants to enroll only those students who have secures more than passing marks then the following code can be written.

Example

<?php
    // Taking user input of marks
    $marks=(int)readline('Enter the marks :');
    if ($marks<=50) {
        trigger_error("Repeat this class since you are fail");
    }
?>

Output

Enter the marks :43

PHP Notice:  Repeat this class since you are fail in /home/jdoodle.php on line 5

PHP Error Handling: Error Logging

A particular file or remote destination can be sent an error log message by using the function error log().

What is better than sending an email full of a description of the error that occurred?

Example

<?php
    // function for checking errors
    function checking_errors($no, $str) {
        echo "Error: [$no] $str";
        echo "technology has been notified";
        error_log("Error: [$no] $str",1,
        "[email protected]","From:[email protected]");
    }
    
    // set error handler
    set_error_handler("checking_errors",E_USER_WARNING);
    
    // triggering error
    $marks=43;
    if ($marks<=50) {
        trigger_error("Repeat this class since you are fail",E_USER_WARNING);
    }
?>

Output

Error: [512] Repeat this class since you are failtechnology has been notified

The mail received by the user will be
Error: [512] Repeat this class since you are fail


All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.