Compdigitec Labs

« | Home | »

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
// Public domain

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 | 18 Comments »

18 Responses to “Stopping syntax error, unexpected ‘;’, expecting T_FUNCTION”

  1. egodi Says:
    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

  2. Gregor Says:
    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?

  3. admin Says:
    September 27th, 2009 at 3:48 pm

    @Gregor:

    Did you by accident put in an extra semi-colon somewhere in your code before the “?>”?

  4. Gregor Says:
    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)

  5. Asdf Says:
    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.

  6. aeou Says:
    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’;
    }
    }
    }

  7. grobemo Says:
    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).

  8. Michael Boggess Says:
    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.

  9. Alex Says:
    July 2nd, 2010 at 10:12 am

    Thanks Asdf.
    Just put a { to the end.

  10. KGF2009 Says:
    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.

  11. David Cobb Says:
    October 26th, 2010 at 4:50 pm

    I agree with the post above and I will find more information from google.

  12. the_smiley Says:
    January 21st, 2011 at 7:14 am

    i had exactly the same problem as the guy with error coming from the line with the ?> tag, though it was an if statement who’s closing bracket i had erroniously commented out

  13. Anne Hedonia Says:
    March 20th, 2011 at 1:11 am

    I LOVE YOU PEOPLE! Added an extra { to the end of my code and it was all fixed. ::MWAH!::

  14. Anne Hedonia Says:
    March 20th, 2011 at 1:11 am

    (Actually, I meant an extra }. Sorry, I’ve been at this too long.

  15. nick Says:
    May 8th, 2011 at 9:43 pm

    Parse error: syntax error, unexpected ‘}’ that shut me down. I can not access the phpbb admin. Anyone see a problem

    {META}
    {SITENAME} • <!-- IF S_IN_MCP -->{L_MCP} • <!-- ELSEIF S_IN_UCP -->{L_UCP} • <!-- ENDIF -->{PAGE_TITLE}






    // <![CDATA[

    if ({S_NEW_PM})
    {
    popup('{UA_POPUP_PM}', 400, 225, '_phpbbprivmsg');
    }

    function popup(url, width, height, name)
    {
    if (!name)
    {
    name = '_popup';
    }

    window.open(url.replace(/&/g, '&'), name, 'height=' + height + ',resizable=yes,scrollbars=yes,width=' + width);
    return false;
    }

    function jumpto()
    {
    var page = prompt('{LA_JUMP_PAGE}:', '{ON_PAGE}');
    var per_page = '{PER_PAGE}';
    var base_url = '{A_BASE_URL}';

    if (page !== null && !isNaN(page) && page == Math.floor(page) && page > 0)
    {
    if (base_url.indexOf('?') == -1)
    {
    document.location.href = base_url + '?start=' + ((page - 1) * per_page);
    }
    else
    {
    document.location.href = base_url.replace(/&/g, '&') + '&start=' + ((page - 1) * per_page);
    }
    }
    }

    /**
    * Find a member
    */
    function find_username(url)
    {
    popup(url, 760, 570, '_usersearch');
    return false;
    }

    /**
    * Mark/unmark checklist
    * id = ID of parent container, name = name prefix, state = state [true/false]
    */
    function marklist(id, name, state)
    {
    var parent = document.getElementById(id);
    if (!parent)
    {
    eval(‘parent = document.’ + id);
    }

    if (!parent)
    {
    return;
    }

    var rb = parent.getElementsByTagName(‘input’);

    for (var r = 0; r < rb.length; r++)
    {
    if (rb[r].name.substr(0, name.length) == name)
    {
    rb[r].checked = state;
    }
    }
    }

    /**
    * Play quicktime file by determining it’s width/height
    * from the displayed rectangle area
    *
    * Only defined if there is a file block present.
    */
    function play_qt_file(obj)
    {
    var rectangle = obj.GetRectangle();

    if (rectangle)
    {
    rectangle = rectangle.split(‘,’)
    var x1 = parseInt(rectangle[0]);
    var x2 = parseInt(rectangle[2]);
    var y1 = parseInt(rectangle[1]);
    var y2 = parseInt(rectangle[3]);

    var width = (x1 < 0) ? (x1 * -1) + x2 : x2 - x1;
    var height = (y1 < 0) ? (y1 * -1) + y2 : y2 - y1;
    }
    else
    {
    var width = 200;
    var height = 0;
    }

    obj.width = width;
    obj.height = height + 16;

    obj.SetControllerVisible(true);

    obj.Play();
    }

    // ]]>


    {L_LOGIN_LOGOUT}    {L_RESTORE_PERMISSIONS}  {L_BOARD_DISABLED}   {PRIVATE_MESSAGE_INFO}, {PRIVATE_MESSAGE_INFO_UNREAD}   {L_REGISTER}
    {L_FAQ}    {L_SEARCH}    {L_MEMBERLIST}    {L_PROFILE}

    {LAST_VISIT_DATE}
    {CURRENT_TIME}



    {L_SEARCH_UNANSWERED} | {L_SEARCH_ACTIVE_TOPICS}{L_SEARCH_UNREAD} | {L_SEARCH_NEW} | {L_SEARCH_SELF}

  16. Joel Says:
    June 10th, 2012 at 12:17 am

    Sometimes you’re just missing a “}”.

  17. Jim Says:
    June 19th, 2012 at 11:59 pm

    This may be the problem, but what line would it complain about? Would it complain about the correct line, the one before, the one after, etc…?

  18. phpdeveloper Says:
    October 25th, 2013 at 3:56 am

    PHP Parse error: syntax error, unexpected ‘;’, expecting T_FUNCTION in /home/nakul/locl.cms.com/connectionManager.php on line 35

    server = $server;
    $this->username = $username;
    $this->password = $password;
    $this->db_name = $db_name;
    $this->connect();
    }
    function connect() {
    mysqli_connect($this->server, $this->username, $this->password) or die(mysql_error());
    mysqli_select_db($this->db_name) or die(mysql_error());
    }
    ?>

Comments