Back to vBulletin 3 Articles

[Tip] Five Things That Might Cause a Syntax Error
by gmarik 14 Jun 2003

Typing Mistakes
We've already mentioned it once, but let's say it again. Check your spelling.

Construct is not closed properly
Most people take care to close loops and conditions, but it's still easy to be caught out when there
are several combined. Look out for something like this:
for ($loop1 = 0; $loop1<10; $loop1++)
{
for ($loop2 = 2; $loop2<20;$loop2++)
{
for ($loop3 = 3; $loop2<30; $loop3++)
{
…Code here…
}
}
It's not always obvious if the loops are closed 10 pages of code later. However, indenting the code
will certainly help, as well as making it easier to understand afterwards. Try formatting your loops
this way:
for ($loop1 = 0; $loop1<10; $loop1++)
{
for ($loop2 = 2; $loop2<20;$loop2++)
{
for ($loop3 = 3; $loop2<30; $loop3++)
{
...Code here...
}
}
This time, it's obvious that we're missing a closing brace.
Missing a semi-colon from the end of a statement

The semi-colon is vital in PHP – leave it off at your peril!

[/b] Getting the name of a function wrong [/b]
It might only be a small misunderstanding, but trying to use some like htmlspecialchar()
instead of htmlspecialchars() will generate a fatal error along the lines of
Fatal error: Call to undefined function: htmlspecialchar().

Not closing a string properly
It seems obvious, but if you fail to add a closing set of quotation marks to your string such as:
echo "Hello world;
PHP will generate a parse error.
The one upside to all of this is that syntax errors are usually quite easy to spot. The parse errors in PHP will
also give the accompanying line number of where the error was generated. Typically this line will be just
before or after the error itself depending on how the erroneous code affects a function, set of braces, or such
like. Also once you do spot them, they are almost all very easy to correct – unlike the next type of error we
will look at.

Marco, Webmaxtor.com

vblts.ru supports vBulletin®, 2022-2024