Troubleshooting a PHP Script

You just can't write scripts without making certain mistakes. The trick is to train yourself to recognize them, roll your eyes, say, "Not again," and just fix them. One error message that you'll see many times is



Parse error: parse error in c:\test.php on line 7



This is PHP's way of saying "Huh?" It means that it doesn't understand something. This message helpfully points to the file and the line number where PHP got confused. Sometimes it's directly pointing at the error, but sometimes PHP's confusion results from an error earlier in the script.



Following are some of the most common errors and how to avoid them.



Missing semicolons


Every PHP statement ends with a semicolon (;). PHP doesn't stop reading a statement until it reaches a semicolon. If you leave out the semicolon at the end of a line, PHP continues reading the statement on the following line. For example, consider the following statement:



$test = 1
echo $test;



These statements don't make sense to PHP; it reads the two lines as one statement, so it complains with an error message, such as the following:



Parse error: parse error in c:\test.php on line 2



This is a very common error. Before you know it, you'll be writing your home address with semicolons at the end of each line.



Not enough equal signs


In a comparison statement, in which you ask whether two values are equal, you need two equal signs in a row. Using one equal sign is a common mistake. It's a perfectly reasonable error because you have been using one equal sign to mean equal since the first grade when you learned that 2 + 2 = 4. This is a difficult mistake to recognize because it doesn't cause an error message. It just makes your script do odd things, like infinite loops or if blocks that never execute. You may be amazed at how long you can stare at something like the following code



$test = 0;
while ( $test = 0 )
{
$test++;
}



and not see why it's looping endlessly.



Missing dollar signs


A missing dollar sign in a variable name is really hard to see, but at least it usually results in an error message so that you know where to look for the problem. It usually results in the old familiar parse error:



Parse error: parse error in test.php on line 7



Troubling quotes


You can have too many, too few, or the wrong kind of quotes. You have too many when you put quotes inside of quotes, such as this example:



$test = "<table width="100%">";



PHP sees the second double quote (") — before 100 — as the ending double quote (") and reads the 1 as an instruction, which makes no sense. Voilà! Another parse error. The line must be either



$test = "<table width='100%'>";



or



$test = "<table width=\"100%\">";



You have too few quotes when you forget to end a quoted string, such as



$test = "<table width='100%'>;



PHP continues reading the lines as part of the quoted string until it encounters another double quote ("), which may not occur for several lines. This is one occasion when the parse error that points to where PHP got confused is not pointing to the actual error. The actual error occurred some lines previously, when you forgot to end the string.



You have the wrong kind of quotes when you use a single quote (') when you meant a double quote (") or vice versa.



Numbered arrays


PHP believes that the first value in an array is numbered zero (0). Of course, humans tend to believe that lists start with the number one (1). This fundamentally different way of viewing lists results in us humans believing an array isn't working correctly when it is indeed working just fine. For example, consider the following statements:



$test = 1;
while ( $test <= 3 )
{
$array[] = $test;
$test++;
}
echo $array[3];



No output (or an error notice) results. You might leap to the conclusion that there is something wrong with the loop. Actually, it's fine. It just results in the following array



$array[0]=1
$array[1]=2
$array[2]=3



and doesn't set anything into $array[3].



Including PHP statements


When a file is read in by using an include statement in a PHP section, it seems reasonable that the statements in the file will be treated as PHP statements. After all, PHP adds the statements to the script at the point where you include them. However, PHP doesn't see it that way. If a file named file1.inc contains the following statements



if ( $test == 1 )
echo "Hi";



and you read it in with the following statements in your main script:



<?php
$test = 1;
include ("file1.inc");
?>



You expect the word Hi to display on the Web page. However, the Web page actually displays this:



if ( $test == 1 ) echo "Hi";



Clearly, the file that is included is seen as HTML. To send Hi to the Web page, file1.inc needs to contain the PHP tags.



<?php
if ( $test == 1 )
echo "Hi";
?>



Missing mates


Parentheses and curly brackets must come in pairs. Opening with a ( that has no closing ) or a { without a } results in an error message. One example of this is when one closing parenthesis appears where two are needed, as in the following statement:



if ( isset($test)



This statement needs a closing parenthesis at the end. It's much more difficult to spot that one of your blocks didn't get closed when you have blocks inside of blocks inside of blocks. For example, consider the following:



while ( $test < 3 )
{
if ( $test2 != "yes" )
{
if ( $test3 > 4 )
{
echo "go";
}
}



You can see there are three opening curly brackets but only two closing ones. Imagine that 100 lines of code are inside these blocks. It can be difficult to spot the problem — especially if you think the last closing bracket is closing the while loop, but PHP sees it as closing the if loop for $test2. Somewhere later in your script, PHP may be using a closing bracket to close the while loop that you aren't even looking at. It can be difficult to trace the problem in a large script.



Indenting blocks makes it easier to see where closing brackets belong. You can also use comments to keep track of where you are, such as



while ( $test < 3 )
{
if ( $test2 != "yes" )
{
if ( $test3 > 4 )
{
echo "go";
} # closing if block for $test3
} # closing if block for $test2
} # closing while block



Confusing parentheses and brackets


Although PHP has no trouble distinguishing between parentheses and curly brackets, your eyes may not always be so reliable — especially if you've been staring at a computer screen for a 10-hour programming marathon. You can easily confuse ( and {. Using the wrong one gets you a parse error message.










dummies

Source:http://www.dummies.com/how-to/content/troubleshooting-a-php-script.html

No comments:

Post a Comment