Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Controlling, supressing, enabling PHP error messages

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)


Back to
Using print_r in PHP to explore mysql database requests
Previous and next
or
Horse's mouth home
Forward to
Abstract classes, Interfaces, PHP and Java
Some other Articles
Wiltshire Unitary News - Chamber of Commerce Intelligence
Melksham Hotel Rooms - pictures
Serialization - storing and reloading objects
Abstract classes, Interfaces, PHP and Java
Controlling, supressing, enabling PHP error messages
Using print_r in PHP to explore mysql database requests
Moving busstop!
Not just a PHP program - a good web application
Tcl scripts / processes on a web server via CGI
Diluting History
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2433_Con ... sages.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb