« Measure CPU temperature and your fan speed in Ubuntu Linux | Home | Compiling PHP-GTK2 Applications into Win32 Executables »
Stopping syntax error, unexpected ‘;’, expecting T_FUNCTION
By admin | July 19, 2008
If you’ve ever been stumped by “Parse error: syntax error, unexpected ‘;’, expecting T_FUNCTION”, you will know that this is a very difficult bug to track. Fortunately, we have found the error for you.
This error message is very misleading, because your actual problem is that you have an extra semi-colon at the end of your functions, like the one below (see line 12):
<?php
// Demo Syntax Error Example
// (C)Copyright 2008 Compdigitec
// Released under GPL
class Demo {
var $a;
var $b;
function problem_here() {
$this->$a = 'Hello World!';
}; # <-- That one
};
?>
Now, the above code looks good, but when you try to run it it PHP spits out the following parse error:
Parse error: syntax error, unexpected ‘;’, expecting T_FUNCTION in *.php on line *
So, to fix it, simply remove the offending semi-colon at the end of your functions. Hope this helped anyone who got stuck with this misleading PHP error.
If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!
Topics: PHP | 10 Comments »

August 27th, 2009 at 1:48 pm
mine is
PHP Parse error: syntax error, unexpected $end, expecting T_FUNCTION in C:\\\test\file.php on line 86
September 26th, 2009 at 4:15 am
Parse error: syntax error, unexpected ‘;’, expecting T_FUNCTION on lin 519.
The line 519 is just ?>
what can I do?
September 27th, 2009 at 3:48 pm
@Gregor:
Did you by accident put in an extra semi-colon somewhere in your code before the “?>”?
September 28th, 2009 at 5:22 am
I checked the whole php file, no ; before ?>. Is it possible to send you the (searchajax).php file?
This are some of the lines of the code:
?>
<input value=" ” class=”form2_submit” type=”submit”>
(<- line 519)
November 11th, 2009 at 2:01 pm
Someone said
“Parse error: syntax error, unexpected ‘;’, expecting T_FUNCTION on lin 519.
The line 519 is just ?>”
The same error to me. It was a } missing at the end, just before ?>. It was something like..
It was a deletion error.
December 23rd, 2009 at 5:44 pm
the following snippet will output the same message (only when inside a class method, not in or out of a procedural function)
class Foo {
public function foo() {
if ($a) {
echo ‘foo’;
if ($b) {
echo ‘bar’;
}
}
}
April 6th, 2010 at 10:59 pm
Thanks! This helped me fix this otherwise puzzling bug.
If you’re getting the “unexpected $end, expecting T_FUNCTION” variation (as egodi does above), it may be because you’ve forgotten to close the class declaration. Try adding a final } after your last method declaration (i.e., after your last function).
April 20th, 2010 at 10:57 pm
I had this error buy it ended up I did not close an if block mid way through the class. Watch out for that stuff.
July 2nd, 2010 at 10:12 am
Thanks Asdf.
Just put a { to the end.
July 12th, 2010 at 1:33 am
Though I’m sure this is rare, but it could happen if you declare a lot of functions in your class, this error will also be thrown if the class itself is not closed.