Let's talk Contact us. No cost. No obligation.

Fill out this form and we will contact you with in 24 hrs.

    captcha

     

    Knowledge Base

    How to Handle Errors in A PHP Script with User Defined Functions

    April 21, 2014

    If we want to show properly formatted error of PHP to the users instead of showing ugly PHP warning or notice messages over the screen, then we can use “set_error_handler” function of PHP.

    Below is the example PHP script of error handling over script.

    function MyError($errno, $errstr, $errfile, $errline)
    {
    echo “abc.com error: [$errno] $errstr
    “;
    echo ” Error on line $errline in $errfile
    “;

    die();
    }

    $error_handling = set_error_handler(“MyError”);

    $conn = mysql_connect(“localhost”,”user”,”pass”) or die(“unable to connect”);
    mysql_select_db(“dbname”,$conn) or die(“unable to select db”);
    echo “veryu good”;

    ?>

    Precaution to be taken while using this function:

    1. This function can’t handle following error types: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT

    2. If errors occur before the script is executed the custom error handler cannot be used since it is not registered at that time

    Potential Use of this function:

    This kind of error handling can be used when we have to perform some action at the time of error occurrence over website. Such that tech team should get notified through an email that system got an error.