Learn How To Handle Error Handling With Example In PHP

admin_img Posted By Bajarangi soft , Posted On 18-09-2020

PHP is used for web development.Error handling in PHP is simple to handle in code. An error message with filename, line number and a message describing the which type of error is sent to the browser.

Error Handling in PHP with Example

Refer Type of Error  in PHP 

PHP Error Handling

When creating code and web applications. If your code lacks error checking code, your program may look improper.
error handling is improtant part in php now we see common error checking method in PHP.

 different error handling:

  • die() Function

  • Custom Error Handling Function



die() Function:
this function execute the code and print the message and exit from current script.

Example(1)

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


Custom Error Handling Function:
Creating a custom error handler is easy understand. We  create a simply function that can be called when an error occurs in PHP.

Syntax 

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


Parameter   Description

error_level          Specifies the error report level for the user-defined error. Must be a value number. See table below for possible error report levels
error_message        Specifies the error message for the user-defined error
error_file           Specifies the filename in which the error occurred
error_line           Specifies the line number in which the error occurred
error_context        Specifies an array containing every variable, and their values, in use when the error occurred


Possiable Error Report levels:

1          E_ERROR            A fatal run-time error. Execution of the script is stopped
2          E_WARNING          A non-fatal run-time error. Execution of the script is not stopped
8          E_NOTICE           A run-time notice. The script found something that might be an error, but could also happen when running a script normally
256        E_USER_ERROR       A fatal user-generated error. This is like an E_ERROR, except it is generated by the PHP script using the function trigger_error()
512        E_USER_WARNING     A non-fatal user-generated warning. This is like an E_WARNING, except it is generated by the PHP script using the function
           trigger_error()                            
1024       E_USER_NOTICE      A user-generated notice. This is like an E_NOTICE, except it is generated by the PHP script using the function trigger_error()
2048       E_STRICT           Not strictly an error.
8191       E_ALL              All errors and warnings



set_error_handler() Function: 
After creating error() function need to set custom error handler because in normal way PHP handles it but if user doing custom error handling then user have to pass out myfunction as a string.

Example(1)

<?php

// Creates  error function which prints message
//to user
function error($error_no, $error_msg) {
    echo "Error: [$error_no] $error_msg ";
    echo "\n  end";

    // When error occurred script has to be stoped
    die();
}

// Setting set_error_handler
set_error_handler("error");

// error
myFunction();
?>

Related Post