Results 1 to 4 of 4

Thread: PHP Error Handling

  1. #1
    Join Date
    Apr 2005
    Posts
    46,704

    Default PHP Error Handling

    PHP Error Handling

    Testing the error handler by trying to output variable that does not exist:

    Code:
    <?php
    //error handler function
    function customError($errno, $errstr)
      {
      echo "<b>Error:</b> [$errno] $errstr";
      }
    
    //set error handler
    set_error_handler("customError");
    
    //trigger error
    echo($test);
    ?>

    Output: Error: [8] Undefined variable: test

  2. #2
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    Trigger an Error


    Error occurs if the "test" variable is bigger than "1":

    Code:
    <?php
    $test=2;
    if ($test>1)
    {
    trigger_error("Value must be 1 or below");
    }
    ?>

  3. #3
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    E_user_warning


    Code:
    <?php
    //error handler function
    function customError($errno, $errstr)
      {
      echo "<b>Error:</b> [$errno] $errstr<br />";
      echo "Ending Script";
      die();
      }
    
    //set error handler
    set_error_handler("customError",E_USER_WARNING);
    
    //trigger error
    $test=2;
    if ($test>1)
      {
      trigger_error("Value must be 1 or below",E_USER_WARNING);
      }
    ?>

    Out put: Error: [512] Value must be 1 or below
    Ending Script

  4. #4
    Join Date
    Apr 2005
    Posts
    46,704

    Default


    Send an Error Message by E-Mail


    Code:
    <?php
    //error handler function
    function customError($errno, $errstr)
      {
      echo "<b>Error:</b> [$errno] $errstr<br />";
      echo "Webmaster has been notified";
      error_log("Error: [$errno] $errstr",1,
      "[email protected]","From: [email protected]");
      }
    
    //set error handler
    set_error_handler("customError",E_USER_WARNING);
    
    //trigger error
    $test=2;
    if ($test>1)
      {
      trigger_error("Value must be 1 or below",E_USER_WARNING);
      }
    ?>
    Out put: Error: [512] Value must be 1 or below
    Webmaster has been notified

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •