Hunting for PHP syntax analysis errors

When writing PHP code, you can make some really hard to track parsing errors if you don't remember which parts you edited earlier. I would be interested to learn about any insightful methods for finding these errors that you might have discovered.

Example: unexpected ';' waiting for T_FUNCTION in someclass.php on line 877

This vague error indicates that you have a surprise; in a class containing 877 lines of PHP code, and the error is definitely not in the last line. Is the error message misleading? Where and how do you start looking, in addition to starting at the top and just trying to find a malicious place to scan each line of code.

+4
source share
7 answers

First of all, I would use an editor that highlights PHP errors on the fly when you enter code, for example, Eclipse PDT (which is quite powerful, actively supported and free, and OSS) - it will help to detect some errors almost immediately, without having to execute code .

If you use it with it, the Subversion plug-in (to integrate SVN access into Eclipse), it can also display what it calls "quick diff": the edge of the modified lines that were not bound to SVN is highlighted - it helps identify that you have changed since the last commit.


Note, however, that since it is based on Eclipse, a rather powerful computer is required (I would say that a dual-core processor with 2 GB of RAM is required, 1 GB is usually not enough if you also want to use some other software at the same time ^^)


Then, when you have been programming in PHP for some time, you will probably be able to understand these messages faster and better and will know where to look :-)

+5
source

If you use some version control system, you can look for differences between the actual and old versions of the file / class.

+2
source

These errors are usually called "{" if, switch, while, do, function, class, etc. not closed.

+2
source

You can also often check syntax, regardless of which editor you use:

  php -l file.php

Please note that I use the word "frequent". If you use vim, you may find the following useful in your .vimrc file:

  map <F12> <ESC>:! php -l% <CR>

Just hit F12 anytime to check the syntax on the fly.

+2
source

Run the code as often as possible as you write it. The fewer lines of code you add between each execution, the less search space for new errors.

If I only edited 5 lines of file 877 since the last page load, the error search would most likely be much faster than if I edited 100 lines.

+1
source

I believe that PHP reporting on syntax errors is very good, but obviously there is a limit to what is possible, given that there can be almost any syntax error. In general, your error is probably close to the specified line. For example, in this case, I would suggest that you do something like:

class Foo { function blah() { }; } 

or perhaps:

 class Foo { protected $foo;; } 

The semicolon is illegal in both of these contexts.

+1
source

Zend Studio has a great debugging tool that allows you to use various parameters, including setting breakpoints, following your code, and checking variables / parameters. Of course, all this is connected with the price. = (

0
source

All Articles