![]() |
|
Article Options |
[Tip] Five Things That Might Cause a Syntax Error
by
![]() 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 No members have liked this post.
|
Views: 2236
|
Comments |
#2
|
||||
|
||||
*Moved to correct forum*
Also please put your code in php boxes just for easier reading ![]() - miSt
__________________
Dean Clatworthy - Web Developer/Designer No members have liked this post.
|
#3
|
||||
|
||||
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 ...
No members have liked this post.
|
#4
|
|||
|
|||
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]. No members have liked this post.
|
#5
|
||||
|
||||
2. It takes me four seconds to type [php] and [code].
![]() No members have liked this post.
|
#6
|
||||
|
||||
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:
__________________
"An It Harm None, Do As Ye Will..." ---The Wiccan Rede Words to live by... No members have liked this post.
Last edited by Cloudrunner; 17 Jun 2003 at 00:39. |
![]() |
«
Previous Article
|
Next Article
»
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Article Options | |
|
|
New To Site? | Need Help? |
All times are GMT. The time now is 02:38.