Variable Debugging Aid
Logic errors are not caused by any sort of failure of the code,but merely a failure of
the programmer to write code that instructs the computer to do exactly what she want-
ed.As a result,errors cannot be detected automatically.You are not told that an error has
occurred,and you are not given a line number where you can look for the problem.
Logic errors are detected only by proper testing.
A logic error such as the previous trivial example is fairly easy to make,but also easy
to correct because the first time your code runs,you will see output other than what
you expected.Most logic errors are a little more insidious.
Troublesome logic errors usually result from developers’assumptions being wrong.
Chapter 24,“Using PHP and MySQL for Large Projects,”recommended using other
developers to review code to suggest additional test cases and using people from the tar-
get audience rather than developers for testing.Assuming that people will enter only cer-
tain types of data is very easy to do and an error that is very easy to leave undetected if
you do your own testing.
Let’s say that you have an Order Quantity text box on a commerce site.Have you
assumed that people will enter only positive numbers? If a visitor enters –10,will your
software refund his credit card with 10 times the price of the item?
Suppose that you have a box to enter a dollar amount.Do you allow people to enter
the amount with or without a dollar sign? Do you allow people to enter numbers with
thousands separated by commas? Some of these things can be checked at the client side
If you are passing information to another page,has it occurred to you that some charac-
ters might have special significance in a URL,such as spaces in the string you are passing?
An infinite number of logic errors is possible.There is no automated way to check for
these errors.The only solution is,first,to try to eliminate assumptions that you have
implicitly coded into the script and,second,test thoroughly with every type of valid and
invalid input possible,ensuring that you get the anticipated result for all.
Variable Debugging Aid
As projects become more complex,having some utility code to help you identify the
cause of errors can be useful.A piece of code that you might find useful is contained in
Listing 25.1.This code echoes the contents of variables passed to your page.
Listing 25.1 dump_variables.php—This Code Can Be Included in Pages to Dump
the Contents of Variables for Debugging
// these lines format the output as HTML comments
// and call dump_array repeatedly
echo “\n<!-- BEGIN VARIABLE DUMP -->\n\n”;
echo “<!-- BEGIN GET VARS -->\n”;