Error reporting in PHP: Part I

By default, PHP generates a lot of helpful information for developers when an error occurs in the execution of a script. This information is much less useful to users, however. In fact, telling users the line number of the error, the function it occurred in, the location of the script on the file system and the exact nature of the error would be considered to compromise the security of the script itself.

Python vs. PHP: Choosing your next project's language

More importantly, it is the developer of the script who needs to be notified of the exact nature of the error, not the user. If only the user receives this error report, the developer will not know of the bug in their script.

This month, we will take a detailed look at error reporting, including how to log as opposed to display errors and how to generate customised errors in a systematic way.

Runtime configuration

There are two ways to have PHP errors logged instead of displayed to the user. The first method is to update the PHP runtime configuration file php.ini, generally stored in /usr/local/lib/php.ini. Locate the lines which relate to error handling (search for 'Error handling'). The following configuration will allow you to log errors.

01 error_reporting = E_ALL
02 display_errors = Off
03 log_errors = On
04 error_log = /path/to/php_error_log

Line 01 tells PHP which errors we are interested in. E_ALL means that all errors should be logged. The default is 'E_ALL & ~E_NOTICE' which means 'all errors except notices'. However, notices are often useful for finding potential bugs in scripts, so turn them on. For more information on the error_reporting variable and its arguments, see www.php.net/manual/en/function.error-reporting.php

Line 02 turns off the display of errors to the user. Line 03 turns on logging of errors. If you do not turn this on, no errors will ever be logged or displayed. The final line tells PHP where to log errors. Change this to a local path.

If you do not have access to php.ini or do not want to modify it, you can change the error reporting configuration on a per script basis. Placing the following in your php script will have the same effect.

01 <?
02 error_reporting(E_ALL);
03 ini_set("display_errors", "off");
04 ini_set("log_errors", "on");
05 ini_set("error_log", "log");
06 ?>

To test this, create and run the following small script:

01 <?
02 error_reporting(E_ALL);
03 ini_set("display_errors", "off");
04 ini_set("log_errors", "on");
05 ini_set("error_log", "log.txt");
06 $a = 1/0;
07 ?>

Line 06 will generate an error because it is illegal to divide by zero in PHP (and, indeed, most programming languages, since the result is undefined in basic mathematics). The result is an error in the file log.txt, which looks something like the following:

[01-Apr-2004 09:27:31] PHP Warning: Division by zero in /path/to/test.php on line 6

Clearly, adding four lines to the beginning of every script is cumbersome and prone to errors. You should use the first method, if possible. Of course, the ability to modify error reporting in this way has its benefits. If you need to debug scripts on systems which has display_errors set to off, you can turn it back on temporarily to debug it. This saves a lot of time looking at files and moving between Web browsers.

Generating your own errors

There are also cases in which you will want to record errors which are not PHP syntactic or semantic errors. For example, say a PHP script is required to process credit card information in real time. The user should be notified if the credit card details are incorrect but not if the processing gateway could not be connected to. Such an error is extremely important for developers and administrators to know about.

The error_log() function provides a range of possibilities for developers to solve such problems. The following script sends an e-mail when such an error takes place:

01 <?
02 if($gateway_error) {<
03 error_log("Could not contact credit card gateway", 1, "you@your.host.com", "Subject: Gateway error");
04 }
05 ?>

Line 03 handles the actual error reporting. The second argument is the message type. In this example we set it to '1', which tells error_log() to send an e-mail. The other possible message types are: '0', which means the error is handled like a normal PHP error; '2', which sends the error to a remote logging host (see www.php.net/manual/en/install.configure.php#install.configure.enable-debugger); or, '3', which appends the error to the file passed as argument three.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

Computerworld
Show Comments

Father’s Day Gift Guide

Brand Post

PC World Evaluation Team Review - MSI GT75 TITAN

"I need power and lots of it. As a Front End Web developer anything less just won’t cut it which is why the MSI GT75 is an outstanding laptop for me. It’s a sleek and futuristic looking, high quality, beast that has a touch of sci-fi flare about it."

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Luke Hill

MSI GT75 TITAN

I need power and lots of it. As a Front End Web developer anything less just won’t cut it which is why the MSI GT75 is an outstanding laptop for me. It’s a sleek and futuristic looking, high quality, beast that has a touch of sci-fi flare about it.

Emily Tyson

MSI GE63 Raider

If you’re looking to invest in your next work horse laptop for work or home use, you can’t go wrong with the MSI GE63.

Laura Johnston

MSI GS65 Stealth Thin

If you can afford the price tag, it is well worth the money. It out performs any other laptop I have tried for gaming, and the transportable design and incredible display also make it ideal for work.

Andrew Teoh

Brother MFC-L9570CDW Multifunction Printer

Touch screen visibility and operation was great and easy to navigate. Each menu and sub-menu was in an understandable order and category

Louise Coady

Brother MFC-L9570CDW Multifunction Printer

The printer was convenient, produced clear and vibrant images and was very easy to use

Edwina Hargreaves

WD My Cloud Home

I would recommend this device for families and small businesses who want one safe place to store all their important digital content and a way to easily share it with friends, family, business partners, or customers.

Featured Content

Product Launch Showcase

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?