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.

558 Chapter 26 Debugging<br />

H<strong>and</strong>ling runtime errors requires a certain amount of forethought—to check for different<br />

types of failure that might occur <strong>and</strong> then take appropriate action. Simulating each<br />

class of runtime error that might occur also takes careful testing.<br />

We do not mean that you need to attempt to simulate every different error that<br />

might occur. <strong>MySQL</strong>, for example, can provide one of around 200 different error numbers<br />

<strong>and</strong> messages.You do need to simulate an error in each function call that is likely to<br />

result in an error <strong>and</strong> an error of each type that is h<strong>and</strong>led by a different block of code.<br />

Failure to Check Input Data<br />

Often you make assumptions about the input data that will be entered by users. If this<br />

data does not fit your expectations, it might cause an error, either a runtime error or a<br />

logic error (detailed in the following section).<br />

A classic example of a runtime error occurs when you are dealing with user input<br />

data <strong>and</strong> you forget to apply addslashes() to it.This means if you have a user with a<br />

name such as O’Grady that contains an apostrophe, you will get an error from the database<br />

function if you use the input in an insert statement inside single quotation marks.<br />

We discuss errors because of assumptions about input data in more detail in the next<br />

section.<br />

Logic Errors<br />

Logic errors can be the hardest type of error to find <strong>and</strong> eliminate.This type of error<br />

occurs when perfectly valid code does exactly what it is instructed to do, but that was<br />

not what the writer intended.<br />

Logic errors can be caused by a simple typing error, such as<br />

for ( $i = 0; $i < 10; $i++ );<br />

{<br />

echo ‘doing something’;<br />

}<br />

This snippet of code is perfectly valid. It follows valid <strong>PHP</strong> syntax. It does not rely on<br />

any external services, so it is unlikely to fail at runtime. Unless you looked at it very<br />

carefully, it probably will not do what you think it will or what the programmer<br />

intended it to do.<br />

At a glance, it looks as if it will iterate through the for loop 10 times, echoing<br />

“doing something” each time.The addition of an extraneous semicolon at the end of<br />

the first line means that the loop has no effect on the following lines.The for loop will<br />

iterate 10 times with no result, <strong>and</strong> then the echo statement will be executed once.<br />

Because this snippet is a perfectly valid, but inefficient, way to write code to achieve<br />

this result, the parser will not complain. Computers are very good at some things, but<br />

they do not have any common sense or intelligence. A computer will do exactly as it is<br />

told.You need to make sure that what you tell it is exactly what you want.

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

Saved successfully!

Ooh no, something went wrong!