Error Handling in PHP

PHP | Error Handling: In this tutorial, we are going to learn about the error handling in PHP with examples using various methods.
Submitted by Sayesha Singh, on June 09, 2020

PHP | 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.

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

1) 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.

Program:

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

Program:

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

2) 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.

Program:

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

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.

Program:

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

3) 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?

Program:

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.