PHP Tip: Use Arrays to Help With Errors!

Sunday, April 5th, 2009

I want to quickly go over today how one can use arrays with php to catch and format your errors. The inspiration behind this post is simple: I often come across php code that checks for a condition (lets say string length) and then on failure it sets a variable (say $string_length) equal to false. Later on in this script, they may check another condition, and then set yet another variable. At the end of the script, these poor souls end up checking each each variable to see if they have failed and then outputting the error message, individually. Blasphemy! Let's look at a better way to do this!

Bad Code

Here is a very brief and simplified example of what I see some people doing:

$string = 'Hello World!';

	if(!isset($string)){
		$not_set = 'TRUE';
	}
	if(!is_numeric($string)){
		$not_numeric = 'TRUE';
	}
	if(strlen($string) > 4){
		$bad_length = 'TRUE';
	}

	if($not_set == 'TRUE'){
		echo 'String has not been set.';
	}
	elseif($not_numeric == 'TRUE') {
		echo 'Strings can only contain numbers.';
	}
	//Painful code checks continue......

Good Coode (or at least much better than above!)

Now lets look at how to use arrays to hold and check for errors in another very simple example.

$string = 'Hello World!';

	if(!isset($string)){
		$errors[] = 'String has not yet been set.';
	}
	if(!is_numeric($string)){
		$errors[] = 'String can only contain numbers.';
	}
	if(strlen($string) > 4){
		$errors[] = 'String can only be a maximum of 4 characters long.';
	}

	if(isset($errors)){
		foreach($errors as $error){
			echo $error . '<br />';
		}
	}

Much better! Keep in mind all of the if statements above are just to demonstrate my point, it could have been written much differently and more simply. The above will output:


String can only contain numbers.
String can only be a maximum of 4 characters long.

And that's all there is to it, there are many more techniques that can be used for error catching and reporting but for the sake of this article we will leave it at that!


If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!

ABOUT THIS AUTHOR

Hi, I'm Drew Douglass and I make sure Dev-Tips.com runs smoothly. I also work for Envato Support and write for NETTUTS.com and ThemeForest.net. I'm passionate about anything web development related, especially php, WordPress, MySQL, and jQuery. Feel free to follow me on twitter.
  1. April 6, 2009 at 3:19 am
    • April 6, 2009 at 2:02 pm
  2. Meshach
    April 6, 2009 at 11:12 am
    • April 6, 2009 at 2:02 pm
  3. April 6, 2009 at 1:16 pm
    • April 6, 2009 at 2:03 pm
  4. April 14, 2009 at 3:00 pm
    • April 14, 2009 at 3:01 pm
    • April 14, 2009 at 3:36 pm
  5. April 17, 2009 at 2:46 am
  6. April 18, 2009 at 10:45 am

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates