Jump to content

parse errors


eddo

Recommended Posts

I'm getting one of those parse errors where the line indicated and the immediately previous line is not the problem. These things happen, again and again in a PHP programmer's life, so I'd like some knowledge that would help me in the future with these kind of situations:

 

1. With a 'parse error', is the mistake always in the line indicated or in a previous line? I understand that 'parse errors' occur on the '1st pass' that a compiler makes thru the code and are comprised pretty much of syntactical and basic control-flow problems in the code type of errors - correct?

 

2. Is it true that in cases where the indicated line number of the code is not the problem, nor is it a missing semicolon in the previous line (or something obvious like that) that the problem is likely a missing paren or closing bracket or brace above the indicated line? (If I get a parse error on line 36, then the error MUST lie in lines 1-37, right?, and is probably of the nature of a missing: ),],},or ' or "?)

 

I currently have one of these errors. I stared at the code so long and hard yesterday that my eyeballs melted out of their sockets and ran down my face - this can be messy. And I'm preparing to start the whole frustrating ordeal over tomorrow. I like a lot of things about PHP, but certainly not how it indicates (so poorly indicates) the nature and location of mistakes in code. Whatever help can be offered in the way of finding the solution to problems of this nature (like what type of things to look for first) would be greatly appreciated on this end, and I'm sure in the community!

Link to comment
Share on other sites

1. Yes

2. Most of the time.

 

Post Code

I want to get good at doing this myself so not inclined to post code just yet. I'm going to start right now, and if I fail again today, I'll probably come back and post the code. I should only have to post the 1st 40 lines or so, and I'll be so pissed off and frustrated all sense of pride will be gone and it will be easy for me to do this. Are there more sophisticated code validators out there which could help one find these kinds of errors (not logical errors, you understand, but syntactical)?

Link to comment
Share on other sites

Are there more sophisticated code validators out there which could help

 

There are editors that highlight syntax to make it easier to see and find matching elements, but there are not any code validators that will tell you what or where something is missing. This is due to the same reason that the php parser often can only report an error on a line following the actual syntax problem. When you leave out some piece of syntax, the parser does not know what you intended and it can only get back on track when it finds some syntax that is invalid within the context of the syntax that is missing an element.

 

For example, suppose you left out a closing quote. The remainder of the current line and any following lines could be part of that quoted string. The parser does not know what you intended. It only gets back on track and reports an error when it finds some syntax that is totally invalid in the current context of a quoted string.

 

Since the only one who knows the intent of what was written and knows which opening and closing syntax belongs together is the programmer, there are no tools available to do this because that would imply that they could read the programmer's mind.

 

The syntax of any programming language has a great amount of symmetry and a relatively small list of rules (compared to a linguistic language.) The way to avoid syntax errors are to learn the syntax. Learn what and where each type of syntax is used and every ', ", (, {, [ is going to have a closing ', ", ), }, ]

Link to comment
Share on other sites

You should use something that does syntax highlighting for writing your code. Something along the lines of "notepad plus"  That way you can see where something went wrong, make it easier to locate the problem. The error you're getting could be mismatched or missing brackets, braces, single quotes, double quotes, semi colon etc..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.