Archive - Originally posted on "The Horse's Mouth" - 2007-08-27 18:49:13 - Graham Ellis
How often have you written a piece of code that's a "spike solution" - it works well on good data - and then spent just as long as you took to do most of the work in fixing errors? I know I have!
These days, I plan my error strategy from minute 0 of hour 0 of day 0. "How to handle errors" is a critical part of application design and ... here's how I do a lot of it in PHP.
a) Set a variable (e.g. $error) to 0 at the start of the code.
b) Whenever an error is found, $error++ and perhaps add a message to $errstr, both of which are global variables declared in all functions that can throw an exception.
c) When the analysis logic is completed, decide whether to display the next screen or recycle the current one with a simple if ($error) test.
Gone are the days of .... if ($_POST[month] < 1 or $_POST[month] > 12 or $_POST[day] < 1 or $_POST[day] > 31 or (not eregi( '^[A-Z]{1,2}[0-9]{1,2}[[:space:]]+' ,$_POST[postcode])) or (not ereg( '[[:digit:]]{4}', $_POST[phone])) or (not ereg('@',$_POST[email)) or $startdate < time() ....
.... Thank Goodness!