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

Maryellen Rose George

Brother PT-P750W

It’s useful for office tasks as well as pragmatic labelling of equipment and storage – just don’t get too excited and label everything in sight!

Cathy Giles

Brother MFC-L8900CDW

The Brother MFC-L8900CDW is an absolute stand out. I struggle to fault it.

Luke Hill


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

Featured Content

Product Launch Showcase

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?