Main Content

Controlling, supressing, enabling PHP error messages

Archive - Originally posted on "The Horse's Mouth" - 2009-10-02 20:37:37 - Graham Ellis

Q: "How can I suppress the PHP error messages?"
Q: "I get no error messages - how can I tell what is going wrong in my PHP"
Q: "Can I replace the PHP error messages with my own"

However PHP is configured, there is no satisfying everyone ... and it's a mystery to many people who install PHP code as to just how it handles errors. Let's find some answers.

First, a simple case of failing code ... as the file "jose.txt" does not exist.

<php
$fh = fopen("jose.txt","r");
print "Hello World";
?>


There are two possible results:
a) An error message from PHP, then the words "Hello World"
b) just the words "Hello World".
Which will it be?

If the php.ini file contains something like:
  error_reporting = E_ALL & ~E_NOTICE
then (unless overridden) you'll get the error message, but if it contains something like:
  error_reporting = 0
you'll get no reports (again .. unless overridden)

The setting in php.ini can be overridden by a directive to set the variable in the httpd.conf file, such as:
  php_value error_reporting -1
which will give you the error messages, or
  php_value error_reporting 0
which will supress reports.

These settings can be further overridden by settings in the .htaccess file - in the same format as the httpd.conf file, but for individual parts of the document tree.

Code changes which can effect the error handling

If the settings about don't provide what you want - if you want to turn standard error messages on and off in a script for different parts of the script, you can do that too:
  php_value error_reporting -1
will turn error reporting on (all error types) whatever is set in php.ini, httpd.conf and .htaccess files, and
  php_value error_reporting 0
will turn error reporting off.

For individual function calls in your code, a leading "@" character can be used to supress error reporting. So:
  $fh = fopen("jose.txt","r");
will generate error messages according to the settings I have described earlier, whereas
  $fh = @fopen("jose.txt","r");
will supress error messages whatever the earlier settings call for.

There are a lot of options there, aren't there? - but I have still not told you how to generate your own error message. The "trick" is that most functions return false if they fail, but another value (usually a true one) if they work correctly. So an @ to suppress the error message, and an if statement to wrap the function call ... and you can provide your own, hopefully more user friendly and application specific, message:

if (! ($fh = @fopen("jose.txt","r"))) {
        print "My own error message<br>";
        }


If you're interested in learning more about PHP coding, have a look at our PHP Programming course. If you're fine with PHP, but baffled by .htaccess, httpd.conf and php.ini ... have a look at our Linux Web Server course which covers the Apache HTTP Server running under Linux or Unix.