PHP Tutorial


PHP 7 Error Handling

Author:- w3alert.com
Updated Date:-25-02-2020

PHP Error Handling

Error handling is an important part of web development. Error handling is almost the same for all programming languages. If your code does not include error handling/checking code during scripting, your script may be unprofessional.
When an error occurs in your PHP script, the default error handling in PHP displays the error message in the web browser with information related to the error message, including the name, line number, and the error type file related to the error.

PHP allows several methods of handling errors, usually using the following two methods, which are the following:

  • PHP simple error handling method: die() function
  • PHP custom errors handling method


Simple error handling method: die() function

The die() function is used to print the output of a message. It combines the function echo and exit functions into one. By this function, if an error occurs in a current script execution, then it exits by stopping the script execution.

Syntax:

    die( $message );

Note: It is used only one parameter. $message parameter signifies the message to printed while exiting from the current script.

Example:

Create a program to print the output of the message with PHP die() Function.

   <?php 

    $file = "w3alert57.txt";

    if (fopen( $file, "r" )) {

        echo "The file is found"; 

    } else {
        
        die( "The file does not found.");
    }
    
    ?>

Output:

It is a runtime error. If the file does not exist, you may receive an error message on the web browser like this:

    Warning: fopen(w3alert57.txt): failed to open stream: No such file or directory in E:\xampp\htdocs\filter.php on line 6

If w3alert57.txt file does not presents then it will display output:

    The file does not found.


PHP custom errors handling method

A custom error handler is a type of user-defined function. This function can be called when an error is found in PHP.

Syntax:

    error_function( error_level, error_message, error_file, error_line, error_context );

There are some parameters used in PHP custom error handling

Error_level: This is the required parameter. It defines the error report level for a user-defined error. It must be an integer.

Error_message: This is the required parameter. It is the error message which the user wants to print.

Error_file: It is an optional parameter. It is used to define the file in which the error occurred.

Error_line: It is also an optional parameter. It is used to define the line number in which the error has occurred.

Error_context: It is also an optional parameter. It is used to define a variable in which every variable and their value is defined when an error occurs.

The following error levels are shown below. There are various types of errors used for user-defined error handlers:

  • 1 : E_ERROR: A fatal runtime error execution of the script has been stopped.
  • 2 : E_WARNING: A non-fatal runtime error execution of the script has been stopped.
  • 4 : E_PARSE: It compiles a timing error generated by the parser.
  • 8 : E_NOTICE: A run-time notice. Something found in the script that may be an error, but It can also occur when the script is run normally.
  • 16 : E_CORE_ERROR: A fatal error that occurs while the beginning startup of the script.
  • 32 : E_CORE_WARNING: A Non-fatal errors that occur while the beginning startup of the script
  • 256 : E_USER_ERROR: A fatal user-generated error. It is like E_ERROR except that the function is created by PHP_script using trigger_error().
  • 512 : E_USER_WARNING: A non-fatal user-generated warning. This is like E_WARNING, except that the function is created by a PHP script using trigger_error().
  • 1024 : E_USER_NOTICE: user-generated information. It acts like E_NOTICE except that it is created by a PHP script using the function trigger_error().
  • 2048 : E_STRICT: Not strictly error.
  • 8191 : E_ALL: All errors and warnings.


Set Error handling: set_error_handler() function

The custom handler needs to be set after the custom handler function is created. The set_error_handler() function is used to set the custom error handler function in which the custom error handler function is passed as a string.

Syntax:

    set_error_handler( $error_handler );

Note: It is used only one parameter. $error_handler parameter signifies the custom error handler function is passed as a string.

Example:

Create a program to create and set the user-defined error handler function.

    <?php 

    //Creat a user defined error handler function.

    function my_custom_error( $error_level, $error_message, $error_file, $error_line, $error_context) {

      echo "<b>Error:</b> [$error_level] $error_level<br>";

      echo "<b>Error:</b> [$error_message] $error_message<br>";

      echo "<b>Error:</b> [$error_file] $error_file<br>";

      echo "<b>Error:</b> [$error_line] $error_line<br>";

      echo "<b>Error:</b> [$error_context] $error_context<br>";

      echo "Ending Script";

      die();

    }

    //Setting set_error_handler

    set_error_handler("my_custom_error");

    // It will generate error

    echo($test);

    ?>