• Archives

  • AddThis Social Bookmark Button

    Add to Technorati Favorites

  • Php error handling

    By krates | 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

    Share and Enjoy:
    • Digg
    • del.icio.us
    • Facebook
    • NewsVine
    • Reddit
    • StumbleUpon
    • YahooMyWeb
    • Google Bookmarks
    • Yahoo! Buzz
    • TwitThis
    • Live
    • LinkedIn
    • Pownce
    • MySpace

    Topics: PHP | No Comments »

    Related Links:

    Comments

    Security Code: