Every sentence must end with a period - Page 2
October 2, 2000
Just as in the English language where every sentence must end with a period,
in PHP every statement must end with a semi-colon ";". The
easiest rule to remember and the most common error. Who has time to count
the semi-colon's when you are trying to meet your deadline. Well if you
don't, the parser does. In some cases a wrong line number may be reported.
<?
$Output="Hello World";//Set the value of Output equal to "Hello World"
echo $Output //Display the contents of the Output variable
//Now We will output some more text
$SomeMoreText="Blah";
print $SomeMoreText; //Identical to the echo function
?>
If you look at line 2:
echo $Output
We forgot the semi-colon to indicate the end of that statement. Something
you may already have noticed. When we run this program the following
error gets reported.
Parse error: parse error, expecting `','' or `';''
in /usr/local/apache/htdocs/test.php on line 8
Our error occurs on line 2 and we are pointed to line 8. Here it may be
obvious to look at the previous instruction line. With much larger
programs this error may not be so obvious.
General Rule of thumb: If the line indicated in the error message
appears to be correct check the previous instruction line (not comment).
If there is no error apparent, comment out the line indicated by the error
message, (place either "C++" style // or shell-style # character
at the beginning of the line), or replace it with something you know is
absolutely correct.
echo 1;
If you still get pointed to the same line you know the error is coming
from a line preceding the indicated line. Continue systematically
commenting lines preceding the one indicated by the error message until it
goes away. Congratulations you've found the offending line.
Debugging PHP: Did You Remember to "Dot All Your I's and Cross All Your T's"?
Debugging PHP: Did You Remember to "Dot All Your I's and Cross All Your T's"?
Spelling mistakes give you a few gray hairs - Page 3
|