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

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; 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

Show Comments

Cool Tech

Crucial Ballistix Elite 32GB Kit (4 x 8GB) DDR4-3000 UDIMM

Learn more >

Gadgets & Things

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

Learn more >

Family Friendly

Lexar® JumpDrive® S57 USB 3.0 flash drive 

Learn more >

Stocking Stuffer

Plox Star Wars Death Star Levitating Bluetooth Speaker

Learn more >

Christmas Gift Guide

Click for more ›

Most Popular Reviews

Latest News Articles


GGG Evaluation Team

Kathy Cassidy


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

Anthony Grifoni


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

Steph Mundell


The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi


The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott


My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?