13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Variable Debugging Aid<br />

559<br />

Logic errors are not caused by any sort of failure of the code, but merely a failure of<br />

the programmer to write code that instructs the computer to do exactly what she wanted.<br />

As a result, errors cannot be detected automatically.You are not told that an error has<br />

occurred, <strong>and</strong> you are not given a line number where you can look for the problem.<br />

Logic errors are detected only by proper testing.<br />

A logic error such as the previous trivial example is fairly easy to make, but also easy<br />

to correct because the first time your code runs, you will see output other than what<br />

you expected. Most logic errors are a little more insidious.<br />

Troublesome logic errors usually result from developers’ assumptions being wrong.<br />

Chapter 25,“Using <strong>PHP</strong> <strong>and</strong> <strong>MySQL</strong> for Large Projects,” recommended using other<br />

developers to review code to suggest additional test cases <strong>and</strong> using people from the target<br />

audience rather than developers for testing. Assuming that people will enter only certain<br />

types of data is very easy to do <strong>and</strong> an error that is very easy to leave undetected if<br />

you do your own testing.<br />

Let’s say that you have an Order Quantity text box on a commerce site. Have you<br />

assumed that people will enter only positive numbers? If a visitor enters –10, will your<br />

software refund his credit card with 10 times the price of the item?<br />

Suppose that you have a box to enter a dollar amount. Do you allow people to enter<br />

the amount with or without a dollar sign? Do you allow people to enter numbers with<br />

thous<strong>and</strong>s separated by commas? Some of these things can be checked at the client side<br />

(using, for example, JavaScript) to take a little load off your server.<br />

If you are passing information to another page, has it occurred to you that some characters<br />

might have special significance in a URL, such as spaces in the string you are passing?<br />

An infinite number of logic errors is possible.There is no automated way to check for<br />

these errors.The only solution is, first, to try to eliminate assumptions that you have<br />

implicitly coded into the script <strong>and</strong>, second, test thoroughly with every type of valid <strong>and</strong><br />

invalid input possible, ensuring that you get the anticipated result for all.<br />

Variable Debugging Aid<br />

As projects become more complex, having some utility code to help you identify the<br />

cause of errors can be useful. A piece of code that you might find useful is contained in<br />

Listing 26.1.This code echoes the contents of variables passed to your page.<br />

Listing 26.1 dump_variables.php—This Code Can Be Included in Pages to Dump<br />

the Contents of Variables for Debugging<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!