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) {

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 PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

Computerworld
Show Comments

Essentials

Lexar® JumpDrive® S57 USB 3.0 flash drive

Learn more >

Microsoft L5V-00027 Sculpt Ergonomic Keyboard Desktop

Learn more >

Mobile

Lexar® JumpDrive® S45 USB 3.0 flash drive 

Learn more >

Exec

Lexar® Professional 1800x microSDHC™/microSDXC™ UHS-II cards 

Learn more >

Lexar® JumpDrive® C20c USB Type-C flash drive 

Learn more >

Audio-Technica ATH-ANC70 Noise Cancelling Headphones

Learn more >

HD Pan/Tilt Wi-Fi Camera with Night Vision NC450

Learn more >

Budget

Back To Business Guide

Click for more ›

Most Popular Reviews

Latest News Articles

Resources

PCW Evaluation Team

Michael Hargreaves

Windows 10 for Business / Dell XPS

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?