Register Members List Search Today's Posts Mark Forums Read

Reply
 
Article Options
[Tip] Five Things That Might Cause a Syntax Error
gmarik
Join Date: May 2002
Posts: 1,288

Nevermind

Mocsow
by gmarik gmarik is offline 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
Views: 2047
Reply With Quote
Comments
  #2  
Old 15 Jun 2003, 11:13
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Real name: Dean Clatworthy
*Moved to correct forum*

Also please put your code in php boxes just for easier reading

- miSt
__________________
Dean Clatworthy - Web Developer/Designer
Reply With Quote
  #3  
Old 15 Jun 2003, 16:57
gmarik's Avatar
gmarik gmarik is offline
 
Join Date: May 2002
Real name: Marco
Mist, there are only five lines of code and I'm lazy. It sends the new added BB code always to the end, you have to cut + paste ...
Reply With Quote
  #4  
Old 15 Jun 2003, 17:35
filburt1 filburt1 is offline
 
Join Date: Feb 2002
One of your tips is indenting. I don't know about you, but:

1. Indenting doesn't work without preformatted tags like code and php
2. It takes me five seconds to type [php] and [code].
Reply With Quote
  #5  
Old 16 Jun 2003, 17:42
gmarik's Avatar
gmarik gmarik is offline
 
Join Date: May 2002
Real name: Marco
2. It takes me four seconds to type [php] and [code]. )
Reply With Quote
  #6  
Old 17 Jun 2003, 01:35
Cloudrunner's Avatar
Cloudrunner Cloudrunner is offline
 
Join Date: May 2003
Real name: Bubba
Actually, I'm blind I think, I see no difference in either of your codes (in fact both would give a parse error as both are only closing two of the loops)....however...indenting and formatting I agree with, but it's much easier for me to read thusly without parse errors:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

__________________
"An It Harm None, Do As Ye Will..."
---The Wiccan Rede
Words to live by...

Last edited by Cloudrunner; 17 Jun 2003 at 01:39.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Article Options

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


New To Site? Need Help?

All times are GMT. The time now is 05:07.

Layout Options | Width: Wide Color: