PHP
  Home arrow PHP arrow Page 4 - Error Handling In PHP (part 2)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Error Handling In PHP (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2002-04-09


    Table of Contents:
  • Error Handling In PHP (part 2)
  • Raising Hell
  • Rolling Back
  • Turning Up The Heat
  • Of Form And Function
  • Buffer Zone
  • Back To Class
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Error Handling In PHP (part 2) - Turning Up The Heat
    ( Page 4 of 8 )

    In addition to catching and displaying errors, PHP's error-handling API also allows you to log errors, either to a default error log, to any other file or as an email message.

    The error_log() function needs a minimum of two arguments: the error message to be logged, and an integer indicating where the message should be sent. There are three possible integer values in PHP 4.x:

    0 - send the message to the system log file (note that you must have logging enabled and a log file specified in your PHP configuration for this to work);

    1 - send the message to the specified email address;

    3 - send the message to the specified file;

    Here's a trivial example which demonstrates how this works:

    <?php // set a variable $temp = 101.6; // test it and log an error // this will only work if // you have "log_errors" and "error_log" set in your php.ini file if ($temp > 98.6) { error_log("Body temperature above normal.", 0); } ?>
    Now, if you look at the system log file after running the script, you'll see something like this:

    [28-Feb-2002 15:50:49] Body temperature above normal.
    You can also write the error message to a file,

    <?php // set a variable $temp = 101.6; // test it and log an error if ($temp > 98.6) { error_log("Body temperature above normal.", 3, "a.out"); } ?>
    or send it out as email.

    <?php // set a variable $temp = 101.6; // test it and log an error if ($temp > 98.6) { error_log("Body temperature above normal.", 1, "administrator@this.body.com"); } ?>
    It's possible to combine this error logging facility with a custom error handler to ensure that all script errors get logged to a file. Here's an example which demonstrates this:

    <?php // custom handler function eh($type, $msg, $file, $line) { // log all errors error_log("$msg (error type $type)", 0); // if fatal error, die() if ($type == E_USER_ERROR) { die($msg); } } // report all errors error_reporting(E_ALL); // define custom handler set_error_handler("eh"); // let's go through the rogues gallery // this will trigger E_NOTICE (undefined variable) echo $someVar; // this will trigger E_WARNING (missing file) include("common.php"); // this will trigger E_USER_NOTICE trigger_error("Time for lunch"); // this will trigger E_USER_WARNING trigger_error("Body temperature above normal", E_USER_WARNING); // this will trigger E_USER_ERROR trigger_error("No brain activity", E_USER_ERROR); ?>
    And here's the output that gets logged to the system log file:

    [28-Feb-2002 16:15:06] Undefined variable: someVar (error type 8) [28-Feb-2002 16:15:06] Failed opening 'common.php' for inclusion (include_path='.;') (error type 2) [28-Feb-2002 16:15:06] Time for lunch (error type 1024) [28-Feb-2002 16:15:06] Body temperature above normal (error type 512) [28-Feb-2002 16:15:06] No brain activity (error type 256)


     
     
    >>> More PHP Articles          >>> More By icarus, (c) Melonfire
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT