Specifications

This does not mean that you need to attempt to simulate every different error that might occur.
MySQL for example can provide one of around 200 different error numbers and messages. You
do need to simulate an error in each function call that is likely to result in an error, and an
error of each type that is handled by a different block of code.
Failure To Check Input Data
Often we make assumptions about the input data that will be entered by users. If this data does
not fit our expectations, it might cause an error, either a runtime error or a logic error (detailed
in the following section).
A classic example of a runtime error occurs when we are dealing with user input data and we
forget to
AddSlashes() to it. This means if we have a user with a name such as OGrady that
contains an apostrophe, we will get an error from the database function.
We will talk more about errors because of assumptions about input data in the next section.
Logic Errors
Logic errors can be the hardest type of error to find and eliminate. This type of error is where
perfectly valid code does exactly what it is instructed to do, but that was not what the writer
intended.
Logic errors can be caused by a simple typing error, such as:
for ( $i = 0; $i < 10; $i++ );
{
echo “doing something<BR>”;
}
This snippet of code is perfectly valid. It follows valid PHP syntax. It does not rely on any
external services, so it is unlikely to fail at runtime. Unless you looked at it very carefully, it
probably will not do what you think it will or what the programmer intended it to do.
At a glance, it looks as if it will iterate through the for loop ten times, echoing “doing
something” each time.
The addition of an extraneous semicolon at the end of the first line means that the loop has no
effect on the following lines. The for loop will iterate ten times with no result, and then the
echo statement will be executed once.
Because this code is a perfectly valid, but inefficient, way to write code to achieve this result,
the parser will not complain. Computers are very good at some things, but they do not have
any common sense or intelligence. A computer will do exactly as it is told. You need to make
sure that what you tell it is exactly what you want.
Debugging
C
HAPTER 23
23
DEBUGGING
485
29 7842 CH23 3/6/01 3:41 PM Page 485