tags:

views:

108

answers:

7

When writing PHP code, you can cause some really hard to track down parsing errors if you don't remember what parts you were editing previously. I'd be interested to hear about any insightful methods to hunt down these errors you may have discovered.

Example: unexpected ';', expecting T_FUNCTION in someclass.php on line 877

This vague error points out that you have unexpected ; in a class containing 877 lines of PHP code, and the error definitely is not on the last line. Is the error message misleading? Where and how do you start looking, besides starting from the top and just trying to find the offending place scanning every row of code.

+2  A: 

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

x2
+5  A: 

First of all, I'd go using an editor that highlight PHP errors on the fly, when you type your code, like, for instance, Eclipse PDT (which is quite powerful, actively maintained, and free, and OSS) -- it will help detect some errors almost immediately, without having to execute the code.

If you are using it along with it's Subversion plug-in (to integrate SVN access in Eclipse), it can also display what it calls "quick diff" : the margin of modified lines which have not been committed to SVN is highlighted -- it help detecting what you changed since the last commit.


Note, though, that, as it's based on Eclipse, requires a quite powerful computer (I'd say a dual-core with 2GB RAM is required, 1 GB generally being not enough if you also want to use some other software at the same time ^^ )


Then, when you have being programming in PHP for quite some time, you'll probably be able to understand those messages faster / better, and will know where to look ;-)

Pascal MARTIN
+1  A: 

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

If I've only edited 5 lines of the 877 line file since the last time I loaded the page, finding the error is likely much faster than if I've edited 100 lines.

acrosman
+2  A: 

These kind of errors are usually by a "{" of an if, switch, while, do, function, class, etc. not closed.

inakiabt
+1  A: 

You can also do a frequent syntax check, no matter what editor you use:

php -l file.php

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 at any time to check syntax on the fly.

Lucas Oman
+1  A: 

I find PHP's reporting on syntax errors to be pretty good, but there is obviously limitation to what is possible, given that a syntax error can be almost anything. In general, your error is probably close to the line reported. For example, in this case, I would assume that you have done something like:

class Foo {
  function blah() {
  };
}

or perhaps:

class Foo {
  protected $foo;;
}

The semicolon is illegal in both of those contexts.

troelskn
A: 

Zend Studio has a great debugging tool that allows you to a variety of options including setting breakpoints, stepping through your code, and inspecting variables / parameters. Ofcourse, all of this comes with a price. =(

jusunlee