Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
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:


<?php
// 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
explode($string);
?>


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:


<?php
// call a non-existent function
// this will generate a fatal error (E_ERROR)
callMeJoe();
?>


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:


<?php
// report only fatal errors
error_reporting(E_ERROR);

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

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


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:


<?php
// report no fatal errors
error_reporting(~E_ERROR);

// call a non-existent function
callMeJoe();
?>


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
 
Search...
 
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
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback