Main Content

PHP - Parse error: syntax error, unexpected $end ...

Archive - Originally posted on "The Horse's Mouth" - 2008-12-23 07:49:20 - Graham Ellis

Have you ever had an irritating PHP message that says "parse error" or "syntax" error and little else - and points to $end / the last line of your PHP file which is (a) perfectly ok and (b) probably HTML anyway? Something like this:

Dorothy:~ grahamellis$ php floppy
 
Parse error: syntax error, unexpected $end in /Users/grahamellis/floppy on line 56
Dorothy:~ grahamellis$


The problem is very likely to be a missing close brace - } - in other words, an if or while, a function or class or something similar isn't completed.

How do you find out just where the problem lies in what could be a big file?

a) Think about what you have just changed since you last ran the PHP - that will give you immediate pointers

b) If you're using an editor such as vi, my personal trick is to add in an extra } at the end of my code, then use the % command to find the matching bracket; it may not be exactly the problem one, but it's a good pointer to the area of concern in the code.