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) {<
03 error_log("Could not contact credit card gateway", 1, "", "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; or, '3', which appends the error to the file passed as argument three.

Join the newsletter!


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.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

Show Comments

Brand Post

Most Popular Reviews

Latest Articles


PCW Evaluation Team

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.

Walid Mikhael

Brother QL-820NWB Professional Label Printer

It’s easy to set up, it’s compact and quiet when printing and to top if off, the print quality is excellent. This is hands down the best printer I’ve used for printing labels.

Ben Ramsden

Sharp PN-40TC1 Huddle Board

Brainstorming, innovation, problem solving, and negotiation have all become much more productive and valuable if people can easily collaborate in real time with minimal friction.

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Featured Content

Product Launch Showcase

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?