• Archives

  • Php error handling

    By admin | May 27, 2008

    Sometimes we need to set our own errors rather than the default one like this.

    Warning: fopen(test.txt) [function.fopen]: failed to open stream: No such file or directory in http://localhost/index.php on line 2

    it is good when you create scripts and sell

    Creating a basic error handler using the die function !!

    if(!file_exists(“text.txt”))
    {
    die(“File does not exist”);
    }
    else
    {
    $file=fopen(“text.txt”,”r”);
    }
    ?>

    Set error handler

    Error handlers are set using

    set_error_handler(“MyErrors”);

    Syntax:

    <?php

    function myerror($var1, $var2)
    {
    echo “<b>Error:</b> [$var1] $var2”;
    }

    set_error_handler(“myerror”);

    echo($var);
    ?>

    Error will be

    Error: [8] Undefined variable: var

    Thanks

    krates

    Topics: PHP | Comments Off on Php error handling

    Related Links: