Home PHP parse/syntax errors; and how to solve them?
Reply: 0

PHP parse/syntax errors; and how to solve them?

user4216 Published in May 22, 2018, 11:26 pm

Everyone runs into syntax errors. Even experienced programmers make typos. For newcomers it's just part of the learning process. However, it's often easy to interpret error messages such as:

PHP Parse error: syntax error, unexpected '{' in index.php on line 20

The unexpected symbol isn't always the real culprit. But the line number gives a rough idea where to start looking.

Always look at the code context. The syntax mistake often hides in the mentioned or in previous code lines. Compare your code against syntax examples from the manual.

While not every case matches the other. Yet there are some general steps to solve syntax mistakes. This references summarized the common pitfalls:

  • Unexpected T_STRING

  • Unexpected T_VARIABLE
    Unexpected '$varname' (T_VARIABLE)


  • Unexpected $end

  • Unexpected T_FUNCTION…

  • Unexpected {

  • Unexpected }

  • Unexpected (

  • Unexpected )

  • Unexpected [

  • Unexpected ]

  • Unexpected T_IF
    Unexpected T_FOREACH
    Unexpected T_FOR
    Unexpected T_WHILE
    Unexpected T_DO
    Unexpected T_PRINT
    Unexpected T_ECHO

  • Unexpected T_LNUMBER

  • Unexpected T_INLINE_HTML…


  • Unexpected T_OBJECT_OPERATOR…

  • Unexpected T_DOUBLE_ARROW…

  • Unexpected T_SL…

  • Unexpected T_BOOLEAN_OR…
    Unexpected T_BOOLEAN_AND…

  • Unexpected T_IS_EQUAL
    Unexpected T_IS_GREATER_OR_EQUAL
    Unexpected T_IS_IDENTICAL
    Unexpected T_IS_NOT_EQUAL
    Unexpected T_IS_NOT_IDENTICAL
    Unexpected T_IS_SMALLER_OR_EQUAL
    Unexpected <
    Unexpected >

  • Unexpected T_NS_SEPARATOR…

  • Unexpected character in input: '\' (ASCII=92) state=1

  • Unexpected 'public' (T_PUBLIC)
    Unexpected 'private' (T_PRIVATE)
    Unexpected 'protected' (T_PROTECTED)
    Unexpected 'final' (T_FINAL)…

  • Unexpected T_STATIC…

  • Unexpected T_CLASS…

  • Unexpected T_DNUMBER

  • Unexpected , (comma)

  • Unpexected . (period)

  • Unexpected ; (semicolon)

  • Unexpected * (asterisk)

  • Unexpected : (colon)

Closely related references:

  • What does this error mean in PHP? (runtime errors)
    • Parse error: syntax error, unexpected T_XXX
    • Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE
    • Parse error: syntax error, unexpected T_VARIABLE
  • What does this symbol mean in PHP? (language tokens)
  • Those “” smart ‘’ quotes mean nothing to PHP


  • The PHP manual on php.net and its various language tokens
  • Or Wikipedia's syntax introduction on PHP.
  • And lastly our php tag-wiki of course.

While Stack Overflow is also welcoming rookie coders, it's mostly targetted at professional programming questions.

  • Answering everyone's coding mistakes and narrow typos is considered mostly off-topic.
  • So please take the time to follow the basic steps, before posting syntax fixing requests.
  • If you still have to, please show your own solving initiative, attempted fixes, and your thought process on what looks or might be wrong.

If your browser displays error messages such as "SyntaxError: illegal character", then it's not actually php-related, but a javascript-syntax error.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.502718 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO