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.
(written 2009-10-02, updated 2009-10-03)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H114 - Sourcing, installing and configuring PHP [192] Current MySQL and PHP paths and upgrades - (2005-01-28)
[316] Development Environments - (2005-05-19)
[481] PHP upgrade - traps to watch - (2005-10-31)
[934] Clustering, load balancing, mod_rewrite and mod_proxy - (2006-11-21)
[1667] Checking server performance for PHP generated pages - (2008-06-06)
[1688] cannot restore segment prot after reloc message - PHP / httpd - (2008-06-26)
[1731] Apache httpd, MySQL, PHP - installation procedure - (2008-08-01)
[1768] What is built in to this httpd and PHP? - (2008-08-23)
[1936] Quick Summary - PHP installation - (2008-12-12)
[2317] How to make a Risotto (PHP build style) - (2009-08-02)
[2589] Your PHP code does not work? Here is where to start looking. - (2010-01-18)
[2646] Compile but do not run PHP - syntax check only - (2010-02-22)
[3132] Virtual Hosting with Apache http server - an overall scheme, and avoiding common pitfalls - (2011-01-14)
[3635] Parse error: parse error, unexpected T_STRING on brand new web site - why? - (2012-03-03)
[3955] Building up from a small PHP setup to an enterprise one - (2012-12-16)
[4319] PHP - some quick fixes if scripts have issues, and how to use our support - (2014-11-19)
[4437] Adding a PHP build option, rotating an image based on camera data, and a new look at thumbnails in PHP - (2015-02-22)
G996 - Well House Consultants - Newsletter Scripts [1001] .pdf files - upload via PHP, store in MySQL, retrieve - (2006-12-19)
[1066] Final, Finally and Finalize - three special words in Java - (2007-02-05)
[1123] mysqldump and mysqlrestore - (2007-03-30)
[1217] What are factory and singleton classes? - (2007-06-04)
[1321] Resetting session based tests in PHP - (2007-08-26)
[1387] Error logging to file not browser in PHP - (2007-10-11)
[1487] Efficient PHP applications - framework and example - (2007-12-28)
[1505] Script to present commonly used images - PHP - (2008-01-13)
[1601] Replacing the last comma with an and - (2008-04-04)
[1665] Factory method example - Perl - (2008-06-04)
[1743] First class functions in Lua lead to powerful OO facilities - (2008-08-07)
[1813] Ajax - going Asyncronous and what it means - (2008-09-28)
[1954] mod_rewrite for newcomers - (2008-12-20)
[2046] Finding variations on a surname - (2009-02-17)
[2145] Using the internet to remotely check for power failure at home (PHP) - (2009-04-29)
[2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
[2360] Error Handling in Lua with assert and pcall - (2009-08-13)
[2539] Changing Images - (2009-12-17)
[3179] Oops - I typed ci not vi, and have lost my file ... - (2011-02-21)
Some other Articles
Wiltshire Unitary News - Chamber of Commerce IntelligenceMelksham Hotel Rooms - picturesSerialization - storing and reloading objectsAbstract classes, Interfaces, PHP and JavaControlling, supressing, enabling PHP error messagesUsing print_r in PHP to explore mysql database requestsMoving busstop!Not just a PHP program - a good web applicationTcl scripts / processes on a web server via CGIDiluting History