05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

time the error happened. Your error handler should check the current level of errors<br />

being reported with error_reporting( ) and act appropriately.<br />

The call to set_error_handler( ) returns the current error handler. You can restore<br />

the previous error handler either by calling set_error_handler( ) with the returned<br />

value when your script is done with its own error handler, or by calling the restore_<br />

error_handler( ) function.<br />

The following code shows how to use an error handler to format and print errors:<br />

function display_error($error, $error_string, $filename, $line, $symbols) {<br />

echo "The error '$error_string' occurred in the file '$filename'<br />

on line $line.";<br />

}<br />

set_error_handler('display_error');<br />

$value = 4 / 0; // divide by zero error<br />

The error 'Division by zero' occurred in the file<br />

'err-2.php' on line 8.<br />

Logging in error handlers<br />

<strong>PHP</strong> provides a built-in function, error_log( ), to log errors to the myriad places<br />

where administrators like to put error logs:<br />

error_log(message, type [, destination [, extra_headers ]]);<br />

The first parameter is the error message. The second parameter specifies where the<br />

error is logged: a value of 0 logs the error via <strong>PHP</strong>’s standard error-logging mechanism;<br />

a value of 1 emails the error to the destination address, optionally adding any<br />

extra_headers to the message; a value of 3 appends the error to the destination file.<br />

To save an error using <strong>PHP</strong>’s logging mechanism, call error_log( ) with a type of 0.By<br />

changing the value of error_log in your php.ini file, you can change which file to log<br />

into. If you set error_log to syslog, the system logger is used instead. For example:<br />

error_log('A connection to the database could not be opened.', 0);<br />

To send an error via email, call error_log( ) with a type of 1. The third parameter is<br />

the email address to which to send the error message, and an optional fourth parameter<br />

can be used to specify additional email headers. Here’s how to send an error<br />

message by email:<br />

error_log('A connection to the database could not be opened.', 1, 'errors@php.net');<br />

Finally, to log to a file, call error_log( ) with a type of 3. The third parameter specifies<br />

the name of the file to log into:<br />

error_log('A connection to the database could not be opened.', 3, '/var/log/php_<br />

errors.log');<br />

Example 13-5 shows an example of an error handler that writes logs into a file and<br />

rotates the log file when it gets above 1 KB.<br />

306 | Chapter 13: Application Techniques<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!