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!
Article Sponsored by:
If you are a coding ninja looking for a quality design, then Clean Cut is for you. Clean Cut is a modern psd template for sale on ThemeForest created by Drew Douglass of Dev-Tips. Clean Cut is perfectly suited for software or web applications.









Yeah, I use array_push though. Don’t know why, but it looks more logical when you are also programming in other languages. Good tip though! This technique saved me a lot of output code in the past.
Hey Gaya
That works too, thanks for sharing your techniques.
Very, very good tip, thanks a lot!
This will help since I am learning PHP.
Your welcome, glad to see someone diving into the world of php
Why not use Exceptions?
if(!isset($string)) {throw new Exception('String has not been set!');
}
They’re really not hard to use, and they offer a little better debugging control, with access to line numbers, file names, stack trace, etc…
There’s nothing wrong with the above example, I just wanted to pose an alternative solution.
You’re absolutely right Jason, thanks very much for your tip. I was hoping people would mention the methods they use as it wasn’t practical to mention every way in this article
Thanks again!
Instead of the foreach statement, I would have used implode(”, $errors); I think explode/implode are my most used functions next to empty/isset.
Ooops, that should read:
implode(‘<br />’, $errors);
That would certainly work just as well, thanks for the tip!
So far so good! And it will help me for the next step. Thanks.
thank for this tips