Logo         Community
The Company
Your Account
Contact Us
PHP 101 (part 12): Bugging Out
Find out how to handle errors in your PHP 5.x scripts.

| Early Warning |

With the theory out of the way, let's now apply it to some examples. Consider the following code snippet:

// initialize the $string variable
$string = 'a string';

// explode() a string
// this will generate a warning or E_WARNING because the number of arguments to explode() is incorrect

If you run this script, you'll get a non-fatal error (E_WARNING), which means that if you had statements following the call to explode(), they would still get executed. Try it for yourself and see!

To generate a fatal error, you need to put in a bit more work. Take a look at this:

// call a non-existent function
// this will generate a fatal error (E_ERROR)

Here, the call to a non-existent function trips all of PHP's alarm wires and generates a fatal error, which immediately stops script execution.

Now, here's the interesting bit. You can control which errors are displayed to the user, by using a built-in PHP function called error_reporting(). This function accepts a named constant, and tells the script to report only errors that match that type. To see this in action, consider the following rewrite of one of the earlier scripts to "hide" non-fatal errors:

// report only fatal errors

// initialize the $string variable
$string = 'string';

// attempt to explode() a string
// this will not generate a warning because only fatal errors are reported

In this case, when the script executes, no warning will be generated even though the call to explode() contains one less argument than it should.

You can use a similar technique to turn off the display of fatal errors:

// report no fatal errors

// call a non-existent function

Keep in mind, though, that just because the error isn't being reported doesn't mean it isn't occurring. Although the script above will not display a visible error message, script execution will still stop at the point of error and statements subsequent to that point will not be executed. error_reporting() gives you control over which errors are displayed; it doesn't prevent the errors themselves.

Note that there are further settings within php.ini that should be used on production sites. You can (and should) turn off display_errors, stipulate an error_log file and switch on log_errors.

Note also that the approach used above to hide error messages, although extremely simple, is not recommended for general use. It is poor programming practice to trap all errors, regardless of type, and ignore them; it is far better - and more professional - to anticipate the likely errors ahead of time, and write defensive code that watches for them and handles them appropriately.

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback