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












551















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 T_CONSTANT_ENCAPSED_STRING
    Unexpected T_ENCAPSED_AND_WHITESPACE


  • 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 ?


  • Unexpected continue (T_CONTINUE)
    Unexpected continue (T_BREAK)
    Unexpected continue (T_RETURN)


  • Unexpected '='


  • Unexpected T_INLINE_HTML…


  • Unexpected T_PAAMAYIM_NEKUDOTAYIM…


  • 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


And:




  • 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.





Syntax errors raised on vendor code: Finally, consider that if the syntax error was not raised by editing your codebase, but after an external vendor package install or upgrade, it could be due to PHP version incompatibility, so check vendor's requirements against your platform setup.










share|improve this question

























  • This isn't enough data to be an answer, but one could write a analyser with parsekit_compile_string, and put more friendly answers on it. If integrated into your IDE, this could be quite informative.

    – Owen Beresford
    Aug 12 '13 at 21:49






  • 3





    You put an impressive amount of work into this. Respect for that. It's probably very good for teachers to learn to fast point out errors or for those creating IDEs or implementing quick fixes. However, IDEs will already effectively do most of this work for you, as @Panique suggests. Additionally, im many cases the start again from scratch is a good option.

    – allprog
    Aug 15 '13 at 12:34













  • Should we add PHP 7 errors preventive?

    – Rizier123
    Oct 20 '15 at 15:31






  • 1





    @Fred-ii- I think most causes are similar to the T_IF / T_FOREACH / ... block. Albeit I wanted to compile a more custom summary for IF/ELSE/ELSEIF questions.

    – mario
    May 28 '16 at 13:45






  • 1





    @mario Don't know how to phrase this, but should this Q&A maybe be a bit rewritten and more structured? (temp comment)

    – Rizier123
    Jun 8 '16 at 14:13
















551















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 T_CONSTANT_ENCAPSED_STRING
    Unexpected T_ENCAPSED_AND_WHITESPACE


  • 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 ?


  • Unexpected continue (T_CONTINUE)
    Unexpected continue (T_BREAK)
    Unexpected continue (T_RETURN)


  • Unexpected '='


  • Unexpected T_INLINE_HTML…


  • Unexpected T_PAAMAYIM_NEKUDOTAYIM…


  • 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


And:




  • 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.





Syntax errors raised on vendor code: Finally, consider that if the syntax error was not raised by editing your codebase, but after an external vendor package install or upgrade, it could be due to PHP version incompatibility, so check vendor's requirements against your platform setup.










share|improve this question

























  • This isn't enough data to be an answer, but one could write a analyser with parsekit_compile_string, and put more friendly answers on it. If integrated into your IDE, this could be quite informative.

    – Owen Beresford
    Aug 12 '13 at 21:49






  • 3





    You put an impressive amount of work into this. Respect for that. It's probably very good for teachers to learn to fast point out errors or for those creating IDEs or implementing quick fixes. However, IDEs will already effectively do most of this work for you, as @Panique suggests. Additionally, im many cases the start again from scratch is a good option.

    – allprog
    Aug 15 '13 at 12:34













  • Should we add PHP 7 errors preventive?

    – Rizier123
    Oct 20 '15 at 15:31






  • 1





    @Fred-ii- I think most causes are similar to the T_IF / T_FOREACH / ... block. Albeit I wanted to compile a more custom summary for IF/ELSE/ELSEIF questions.

    – mario
    May 28 '16 at 13:45






  • 1





    @mario Don't know how to phrase this, but should this Q&A maybe be a bit rewritten and more structured? (temp comment)

    – Rizier123
    Jun 8 '16 at 14:13














551












551








551


207






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 T_CONSTANT_ENCAPSED_STRING
    Unexpected T_ENCAPSED_AND_WHITESPACE


  • 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 ?


  • Unexpected continue (T_CONTINUE)
    Unexpected continue (T_BREAK)
    Unexpected continue (T_RETURN)


  • Unexpected '='


  • Unexpected T_INLINE_HTML…


  • Unexpected T_PAAMAYIM_NEKUDOTAYIM…


  • 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


And:




  • 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.





Syntax errors raised on vendor code: Finally, consider that if the syntax error was not raised by editing your codebase, but after an external vendor package install or upgrade, it could be due to PHP version incompatibility, so check vendor's requirements against your platform setup.










share|improve this question
















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 T_CONSTANT_ENCAPSED_STRING
    Unexpected T_ENCAPSED_AND_WHITESPACE


  • 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 ?


  • Unexpected continue (T_CONTINUE)
    Unexpected continue (T_BREAK)
    Unexpected continue (T_RETURN)


  • Unexpected '='


  • Unexpected T_INLINE_HTML…


  • Unexpected T_PAAMAYIM_NEKUDOTAYIM…


  • 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


And:




  • 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.





Syntax errors raised on vendor code: Finally, consider that if the syntax error was not raised by editing your codebase, but after an external vendor package install or upgrade, it could be due to PHP version incompatibility, so check vendor's requirements against your platform setup.







php parsing debugging syntax-error






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 29 '18 at 2:24


























community wiki





29 revs, 11 users 46%
mario














  • This isn't enough data to be an answer, but one could write a analyser with parsekit_compile_string, and put more friendly answers on it. If integrated into your IDE, this could be quite informative.

    – Owen Beresford
    Aug 12 '13 at 21:49






  • 3





    You put an impressive amount of work into this. Respect for that. It's probably very good for teachers to learn to fast point out errors or for those creating IDEs or implementing quick fixes. However, IDEs will already effectively do most of this work for you, as @Panique suggests. Additionally, im many cases the start again from scratch is a good option.

    – allprog
    Aug 15 '13 at 12:34













  • Should we add PHP 7 errors preventive?

    – Rizier123
    Oct 20 '15 at 15:31






  • 1





    @Fred-ii- I think most causes are similar to the T_IF / T_FOREACH / ... block. Albeit I wanted to compile a more custom summary for IF/ELSE/ELSEIF questions.

    – mario
    May 28 '16 at 13:45






  • 1





    @mario Don't know how to phrase this, but should this Q&A maybe be a bit rewritten and more structured? (temp comment)

    – Rizier123
    Jun 8 '16 at 14:13



















  • This isn't enough data to be an answer, but one could write a analyser with parsekit_compile_string, and put more friendly answers on it. If integrated into your IDE, this could be quite informative.

    – Owen Beresford
    Aug 12 '13 at 21:49






  • 3





    You put an impressive amount of work into this. Respect for that. It's probably very good for teachers to learn to fast point out errors or for those creating IDEs or implementing quick fixes. However, IDEs will already effectively do most of this work for you, as @Panique suggests. Additionally, im many cases the start again from scratch is a good option.

    – allprog
    Aug 15 '13 at 12:34













  • Should we add PHP 7 errors preventive?

    – Rizier123
    Oct 20 '15 at 15:31






  • 1





    @Fred-ii- I think most causes are similar to the T_IF / T_FOREACH / ... block. Albeit I wanted to compile a more custom summary for IF/ELSE/ELSEIF questions.

    – mario
    May 28 '16 at 13:45






  • 1





    @mario Don't know how to phrase this, but should this Q&A maybe be a bit rewritten and more structured? (temp comment)

    – Rizier123
    Jun 8 '16 at 14:13

















This isn't enough data to be an answer, but one could write a analyser with parsekit_compile_string, and put more friendly answers on it. If integrated into your IDE, this could be quite informative.

– Owen Beresford
Aug 12 '13 at 21:49





This isn't enough data to be an answer, but one could write a analyser with parsekit_compile_string, and put more friendly answers on it. If integrated into your IDE, this could be quite informative.

– Owen Beresford
Aug 12 '13 at 21:49




3




3





You put an impressive amount of work into this. Respect for that. It's probably very good for teachers to learn to fast point out errors or for those creating IDEs or implementing quick fixes. However, IDEs will already effectively do most of this work for you, as @Panique suggests. Additionally, im many cases the start again from scratch is a good option.

– allprog
Aug 15 '13 at 12:34







You put an impressive amount of work into this. Respect for that. It's probably very good for teachers to learn to fast point out errors or for those creating IDEs or implementing quick fixes. However, IDEs will already effectively do most of this work for you, as @Panique suggests. Additionally, im many cases the start again from scratch is a good option.

– allprog
Aug 15 '13 at 12:34















Should we add PHP 7 errors preventive?

– Rizier123
Oct 20 '15 at 15:31





Should we add PHP 7 errors preventive?

– Rizier123
Oct 20 '15 at 15:31




1




1





@Fred-ii- I think most causes are similar to the T_IF / T_FOREACH / ... block. Albeit I wanted to compile a more custom summary for IF/ELSE/ELSEIF questions.

– mario
May 28 '16 at 13:45





@Fred-ii- I think most causes are similar to the T_IF / T_FOREACH / ... block. Albeit I wanted to compile a more custom summary for IF/ELSE/ELSEIF questions.

– mario
May 28 '16 at 13:45




1




1





@mario Don't know how to phrase this, but should this Q&A maybe be a bit rewritten and more structured? (temp comment)

– Rizier123
Jun 8 '16 at 14:13





@mario Don't know how to phrase this, but should this Q&A maybe be a bit rewritten and more structured? (temp comment)

– Rizier123
Jun 8 '16 at 14:13












15 Answers
15






active

oldest

votes


















247














What are the syntax errors?



PHP belongs to the C-style and imperative programming languages. It has rigid grammar rules, which it cannot recover from when encountering misplaced symbols or identifiers. It can't guess your coding intentions.



Function definition syntax abstract



Most important tips



There are a few basic precautions you can always take:




  • Use proper code indentation, or adopt any lofty coding style.
    Readability prevents irregularities.



  • Use an IDE or editor for PHP with syntax highlighting.
    Which also help with parentheses/bracket balancing.



    Expected: semicolon



  • Read the language reference and examples in the manual.
    Twice, to become somewhat proficient.



How to interpret parser errors



A typical syntax error message reads:




Parse error: syntax error, unexpected T_STRING, expecting ';' in file.php on line 217




Which lists the possible location of a syntax mistake. See the mentioned file name and line number.



A moniker such as T_STRING explains which symbol the parser/tokenizer couldn't process finally. This isn't necessarily the cause of the syntax mistake, however.



It's important to look into previous code lines as well. Often syntax errors are just mishaps that happened earlier. The error line number is just where the parser conclusively gave up to process it all.



Solving syntax errors



There are many approaches to narrow down and fix syntax hiccups.





  • Open the mentioned source file. Look at the mentioned code line.




    • For runaway strings and misplaced operators, this is usually where you find the culprit.


    • Read the line left to right and imagine what each symbol does.





  • More regularly you need to look at preceding lines as well.




    • In particular, missing ; semicolons are missing at the previous line ends/statement. (At least from the stylistic viewpoint. )


    • If { code blocks } are incorrectly closed or nested, you may need to investigate even further up the source code. Use proper code indentation to simplify that.





  • Look at the syntax colorization!




    • Strings and variables and constants should all have different colors.


    • Operators +-*/. should be tinted distinct as well. Else they might be in the wrong context.


    • If you see string colorization extend too far or too short, then you have found an unescaped or missing closing " or ' string marker.


    • Having two same-colored punctuation characters next to each other can also mean trouble. Usually, operators are lone if it's not ++, --, or parentheses following an operator. Two strings/identifiers directly following each other are incorrect in most contexts.




  • Whitespace is your friend.
    Follow any coding style.



  • Break up long lines temporarily.




    • You can freely add newlines between operators or constants and strings. The parser will then concretize the line number for parsing errors. Instead of looking at the very lengthy code, you can isolate the missing or misplaced syntax symbol.


    • Split up complex if statements into distinct or nested if conditions.


    • Instead of lengthy math formulas or logic chains, use temporary variables to simplify the code. (More readable = fewer errors.)



    • Add newlines between:




      1. The code you can easily identify as correct,

      2. The parts you're unsure about,

      3. And the lines which the parser complains about.



      Partitioning up long code blocks really helps to locate the origin of syntax errors.






  • Comment out offending code.




    • If you can't isolate the problem source, start to comment out (and thus temporarily remove) blocks of code.


    • As soon as you got rid of the parsing error, you have found the problem source. Look more closely there.


    • Sometimes you want to temporarily remove complete function/method blocks. (In case of unmatched curly braces and wrongly indented code.)


    • When you can't resolve the syntax issue, try to rewrite the commented out sections from scratch.





  • As a newcomer, avoid some of the confusing syntax constructs.




    • The ternary ? : condition operator can compact code and is useful indeed. But it doesn't aid readability in all cases. Prefer plain if statements while unversed.


    • PHP's alternative syntax (if:/elseif:/endif;) is common for templates, but arguably less easy to follow than normal { code } blocks.





  • The most prevalent newcomer mistakes are:




    • Missing semicolons ; for terminating statements/lines.


    • Mismatched string quotes for " or ' and unescaped quotes within.


    • Forgotten operators, in particular for the string . concatenation.


    • Unbalanced ( parentheses ). Count them in the reported line. Are there an equal number of them?





  • Don't forget that solving one syntax problem can uncover the next.




    • If you make one issue go away, but other crops up in some code below, you're mostly on the right path.


    • If after editing a new syntax error crops up in the same line, then your attempted change was possibly a failure. (Not always though.)





  • Restore a backup of previously working code, if you can't fix it.




    • Adopt a source code versioning system. You can always view a diff of the broken and last working version. Which might be enlightening as to what the syntax problem is.






  • Invisible stray Unicode characters: In some cases, you need to use a hexeditor or different editor/viewer on your source. Some problems cannot be found just from looking at your code.




    • Try grep --color -P -n "[x80-xFF]" file.php as the first measure to find non-ASCII symbols.


    • In particular BOMs, zero-width spaces, or non-breaking spaces, and smart quotes regularly can find their way into the source code.





  • Take care of which type of linebreaks are saved in files.




    • PHP just honors n newlines, not r carriage returns.


    • Which is occasionally an issue for MacOS users (even on OS  X for misconfigured editors).


    • It often only surfaces as an issue when single-line // or # comments are used. Multiline /*...*/ comments do seldom disturb the parser when linebreaks get ignored.





  • If your syntax error does not transmit over the web:
    It happens that you have a syntax error on your machine. But posting the very same file online does not exhibit it anymore. Which can only mean one of two things:




    • You are looking at the wrong file!


    • Or your code contained invisible stray Unicode (see above).
      You can easily find out: Just copy your code back from the web form into your text editor.





  • Check your PHP version. Not all syntax constructs are available on every server.




    • php -v for the command line interpreter


    • <?php phpinfo(); for the one invoked through the webserver.




    Those aren't necessarily the same. In particular when working with frameworks, you will them to match up.



  • Don't use PHP's reserved keywords as identifiers for functions/methods, classes or constants.


  • Trial-and-error is your last resort.



If all else fails, you can always google your error message. Syntax symbols aren't as easy to search for (Stack Overflow itself is indexed by SymbolHound though). Therefore it may take looking through a few more pages before you find something relevant.



Further guides:





  • PHP Debugging Basics by David Sklar


  • Fixing PHP Errors by Jason McCreary


  • PHP Errors – 10 Common Mistakes by Mario Lurig

  • Common PHP Errors and Solutions

  • How to Troubleshoot and Fix your WordPress Website


  • A Guide To PHP Error Messages For Designers - Smashing Magazine


White screen of death



If your website is just blank, then typically a syntax error is the cause.
Enable their display with:




  • error_reporting = E_ALL

  • display_errors = 1


In your php.ini generally, or via .htaccess for mod_php,
or even .user.ini with FastCGI setups.



Enabling it within the broken script is too late because PHP can't even interpret/run the first line. A quick workaround is crafting a wrapper script, say test.php:



<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
include("./broken-script.php");


Then invoke the failing code by accessing this wrapper script.



It also helps to enable PHP's error_log and look into your webserver's error.log when a script crashes with HTTP 500 responses.






share|improve this answer


























  • error_reporting(E_ALL | E_STRICT); for earlier versions of PHP

    – Geo
    Aug 15 '13 at 21:42






  • 2





    Some IDEs (like NetBeans) not only support syntax highlighting but also code formatting. If you get into the habit of formatting your code properly and asking the IDE to reformat just in case from time to time you may catch hard to spot problems like unmatched braces.

    – Josep Valls
    May 21 '15 at 19:20



















98





+250









I think this topic is totally overdiscussed/overcomplicated. Using an IDE is THE way to go to completely avoid any syntax errors. I would even say that working without an IDE is kind of unprofessional. Why? Because modern IDEs check your syntax after every character you type. When you code and your entire line turns red, and a big warning notice shows you the exact type and the exact position of the syntax error, then there's absolutely no need to search for another solution.



Using a syntax-checking IDE means:



You'll (effectively) never run into syntax errors again, simply because you see them right as you type. Seriously.



Excellent IDEs with syntax check (all of them are available for Linux, Windows and Mac):





  1. NetBeans [free]


  2. PHPStorm [$199 USD]


  3. Eclipse with PHP Plugin [free]


  4. Sublime [$80 USD] (mainly a text editor, but expandable with plugins, like PHP Syntax Parser)






share|improve this answer





















  • 1





    It is obviously. However, relisting IDEs here, can you elaborate a bit where they differ in their syntax helpfulness? Sublime is mostly an editor, not IDE; but then more pretty and snappy; does primarily just syntax highlighing but's also veritable at bracket matching. It easily discovers T_CONSTANT_AND_ENCAPSED errors instantly for example, unlike PHPStorm; which however does more squiggly lines for inline errors. NetBeans´ syntax hints used to be more cryptic than PHPs even (relisting allowed constructs rather). Can you share your experience on pros/cons; is your favorite Eclipse/PDT or..?

    – mario
    Aug 12 '13 at 20:31











  • @mario I think you are really deep into the topic so i really dont want to say anything wrong here, but all the code I (and my team mates, friends who code, freelance partners) have ever written in an IDE never ever was executed with a syntax error. So I think at least Netbeans/PHPStorm's syntax check is extremely powerful. But maybe I've misread your question. Gimme some hours ... ;)

    – Sliq
    Aug 12 '13 at 21:03











  • Your answer is already spot on. Would fit 99% of our questions. However for the context here I'd like a trade-off consideration on which IDE provides the more newbie-friendly tooltips. It's probably minor to us, colorization and squiggly lines being sufficient if you're versed enough. But I presume the differences could be more significant to beginners.

    – mario
    Aug 12 '13 at 21:29











  • Sometimes an IDE is not a feasible option. For example, making quick edits to a WordPress theme or plugin. Yes, I could copy all the code into an IDE, but then I have to open it up, paste it all in there, set headers and all that other time wasting crap, when I'm just hoping for a quick edit. Now, if you're developing new features or starting from scratch, then, yes, do it in an IDE. You won't regret taking that bit of extra time at the start to set it up.

    – fredsbend
    Mar 6 '17 at 16:22













  • I see IDE as a trailer, not just a toolbox. It might not FIX but it can help you find and prevent syntax errors. Many answer here seems to say that if you keep you code clean you have less chance to make an error and are easier to spot. Well with auto-indentation, code hints, variable occurrence, auto-closing brackets and auto-formatting saves me many typos a day and is the main advantage why i use one. This is not counting everything else beyond the scope of this question (debugger, database connector, uml diagram etc.) IDE will save you time and will prevent more than just syntax errors.

    – Louis Loudog Trottier
    Mar 27 '17 at 4:48



















48














Unexpected [



These days, the unexpected [ array bracket is commonly seen on outdated PHP versions. The short array syntax is available since PHP >= 5.4. Older installations only support array().



$php53 = array(1, 2, 3);
$php54 = [1, 2, 3];



Array function result dereferencing is likewise not available for older PHP versions:



$result = get_whatever()["key"];



Reference - What does this error mean in PHP? - "Syntax error, unexpected [" shows the most common and practical workarounds.



Though, you're always better off just upgrading your PHP installation. For shared webhosting plans, first research if e.g. SetHandler php56-fcgi can be used to enable a newer runtime.



See also:




  • PHP syntax for dereferencing function result → possible as of PHP 5.4

  • PHP syntax error, unexpected '['

  • Shorthand for arrays: is there a literal syntax like {} or ?

  • PHP 5.3.10 vs PHP 5.5.3 syntax error unexpected '['

  • PHP Difference between array() and

  • PHP Array Syntax Parse Error Left Square Bracket "["


BTW, there are also preprocessors and PHP 5.4 syntax down-converters if you're really clingy with older + slower PHP versions.



Other causes for Unexpected [ syntax errors



If it's not the PHP version mismatch, then it's oftentimes a plain typo or newcomer syntax mistake:





  • You can't use array property declarations/expressions in classes, not even in PHP 7.



    protected $var["x"] = "Nope";




  • Confusing [ with opening curly braces { or parentheses ( is a common oversight.



    foreach [$a as $b)



    Or even:



    function foobar[$a, $b, $c] {




  • Or trying to dereference constants (before PHP 5.6) as arrays:



    $var = const[123];



    At least PHP interprets that const as a constant name.



    If you meant to access an array variable (which is the typical cause here), then add the leading $ sigil - so it becomes a $varname.




  • You are trying to use the global keyword on a member of an associative array. This is not valid syntax:



    global $var['key'];






Unexpected ] closing square bracket



This is somewhat rarer, but there are also syntax accidents with the terminating array ] bracket.





  • Again mismatches with ) parentheses or } curly braces are common:



    function foobar($a, $b, $c] {




  • Or trying to end an array where there isn't one:



    $var = 2];


    Which often occurs in multi-line and nested array declarations.



    $array = [1,[2,3],4,[5,6[7,[8],[9,10]],11],12]],15];



    If so, use your IDE for bracket matching to find any premature ] array closure. At the very least use more spacing and newlines to narrow it down.








share|improve this answer

































    42














    Unexpected T_VARIABLE



    An "unexpected T_VARIABLE" means that there's a literal $variable name, which doesn't fit into the current expression/statement structure.



    purposefully abstract/inexact operator+$variable diagram





    1. Missing semicolon



      It most commonly indicates a missing semicolon in the previous line. Variable assignments following a statement are a good indicator where to look:




      func1()
      $var = 1 + 2; # parse error in line +2



    2. String concatenation



      A frequent mishap are string concatenations with forgotten . operator:




      print "Here comes the value: " $value;


      Btw, you should prefer string interpolation (basic variables in double quotes) whenever that helps readability. Which avoids these syntax issues.




      String interpolation is a scripting language core feature. No shame in utilizing it. Ignore any micro-optimization advise about variable . concatenation being faster. It's not.





    3. Missing expression operators



      Of course the same issue can arise in other expressions, for instance arithmetic operations:




      print 4 + 7 $var;


      PHP can't guess here if the variable should have been added, subtracted or compared etc.




    4. Lists



      Same for syntax lists, like in array populations, where the parser also indicates an expected comma , for example:




      $var = array("1" => $val, $val2, $val3 $val4);


      Or functions parameter lists:




      function myfunc($param1, $param2 $param3, $param4)


      Equivalently do you see this with list or global statements, or when lacking a ; semicolon in a for loop.




    5. Class declarations



      This parser error also occurs in class declarations. You can only assign static constants, not expressions. Thus the parser complains about variables as assigned data:



      class xyz {      ⇓
      var $value = $_GET["input"];


      Unmatched } closing curly braces can in particular lead here. If a method is terminated too early (use proper indentation!), then a stray variable is commonly misplaced into the class declaration body.




    6. Variables after identifiers



      You can also never have a variable follow an identifier directly:




      $this->myFunc$VAR();


      Btw, this is a common example where the intention was to use variable variables perhaps. In this case a variable property lookup with $this->{"myFunc$VAR"}(); for example.




      Take in mind that using variable variables should be the exception. Newcomers often try to use them too casually, even when arrays would be simpler and more appropriate.





    7. Missing parens after language constructs



      Hasty typing may lead to forgotten opening parenthesis
      for if and for and foreach statements:




      foreach $array as $key) {


      Solution: add the missing opening ( between statement and variable.




    8. Else does not expect conditions




      else ($var >= 0)


      Solution: Remove the conditions from else or use elseif.




    9. Need brackets for closure




      function() uses $var {}


      Solution: Add brackets around $var.




    10. Invisible whitespace



      As mentioned in the reference answer on "Invisible stray Unicode" (such as a non-breaking space), you might also see this error for unsuspecting code like:



      <?php

      $var = new PDO(...);


      It's rather prevalent in the start of files and for copy-and-pasted code. Check with a hexeditor, if your code does not visually appear to contain a syntax issue.




    See also




    • Search: unexpected T_VARIABLE






    share|improve this answer

































      31
















      Unexpected T_CONSTANT_ENCAPSED_STRING
      Unexpected T_ENCAPSED_AND_WHITESPACE



      The unwieldy names T_CONSTANT_ENCAPSED_STRING and T_ENCAPSED_AND_WHITESPACE refer to quoted "string" literals.



      They're used in different contexts, but the syntax issue are quite similar. T_ENCAPSED… warnings occur in double quoted string context, while T_CONSTANT… strings are often astray in plain PHP expressions or statements.





      1. Incorrect variable interpolation



        And it comes up most frequently for incorrect PHP variable interpolation:



                                  ⇓     ⇓
        echo "Here comes a $wrong['array'] access";


        Quoting arrays keys is a must in PHP context. But in double quoted strings (or HEREDOCs) this is a mistake. The parser complains about the contained single quoted 'string', because it usually expects a literal identifier / key there.



        More precisely it's valid to use PHP2-style simple syntax within double quotes for array references:



        echo "This is only $valid[here] ...";


        Nested arrays or deeper object references however require the complex curly string expression syntax:



        echo "Use {$array['as_usual']} with curly syntax.";


        If unsure, this is commonly safer to use. It's often even considered more readable. And better IDEs actually use distinct syntax colorization for that.




      2. Missing concatenation



        If a string follows an expression, but lacks a concatenation or other operator, then you'll see PHP complain about the string literal:




        print "Hello " . WORLD " !";


        While it's obvious to you and me, PHP just can't guess that the string was meant to be appended there.




      3. Confusing string quote enclosures



        The same syntax error occurs when confounding string delimiters. A string started by a single ' or double " quote also ends with the same.




        print "<a href="' . $link . '">click here</a>";
        ⌞⎽⎽⎽⎽⎽⎽⎽⎽⌟⌞⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⌟⌞⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⌟


        That example started with double quotes. But double quotes were also destined for the HTML attributes. The intended concatenation operator within however became interpreted as part of a second string in single quotes.




        Tip: Set your editor/IDE to use slightly distinct colorization for single and double quoted strings. (It also helps with application logic to prefer e.g. double quoted strings for textual output, and single quoted strings only for constant-like values.)




        This is a good example where you shouldn't break out of double quotes in the first place. Instead just use proper " escapes for the HTML attributes´ quotes:



        print "<a href="{$link}">click here</a>";


        While this can also lead to syntax confusion, all better IDEs/editors again help by colorizing the escaped quotes differently.




      4. Missing opening quote



        Equivalently are forgotten opening "/' quotes a recipe for parser errors:




        make_url(login', 'open');


        Here the ', ' would become a string literal after a bareword, when obviously login was meant to be a string parameter.




      5. Array lists



        If you miss a , comma in an array creation block, the parser will see two consecutive strings:



        array(               ⇓
        "key" => "value"
        "next" => "....",
        );


        Note that the last line may always contain an extra comma, but overlooking one in between is unforgivable. Which is hard to discover without syntax highlighting.




      6. Function parameter lists



        Same thing for function calls:




        myfunc(123, "text", "and" "more")



      7. Runaway strings



        A common variation are quite simply forgotten string terminators:




        mysql_evil("SELECT * FROM stuffs);
        print "'ok'";



        Here PHP complains about two string literals directly following each other. But the real cause is the unclosed previous string of course.




      See also




      • Interpolation (double quoted string) of Associative Arrays in PHP

      • PHP - syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

      • Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in PHP

      • Unexpected T_CONSTANT_ENCAPSED_STRING error in SQL Query






      share|improve this answer

































        22














        Unexpected T_STRING



        T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.





        1. Misquoted strings



          This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:



                         ⇓                  ⇓
          echo "<a href="http://example.com">click here</a>";


          Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping " double quotes, or ' single quotes - depending on which was used as string enclosure.




          • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.

          • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.

          • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.


          See also What is the difference between single-quoted and double-quoted strings in PHP?.




        2. Unclosed strings



          If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:




          echo "Some text", $a_variable, "and some runaway string ;
          success("finished");



          It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.




        3. Non-programming string quotes



          If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:



          $text = ’Something something..’ + ”these ain't quotes”;


          Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example ”these is interpreted as a constant identifier. But any following text literal is then seen as a bareword/T_STRING by the parser.




        4. The missing semicolon; again



          If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:




          func1()
          function2();


          PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.




        5. Short open tags and <?xml headers in PHP scripts



          This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:




          <?xml version="1.0"?>


          PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.




        6. Invisible Unicode characters



          A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:



          <?php
          print 123;


          You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.



          You can narrow it down with with adding redundant ; statement separators at each line start:



          <?php
          ;print 123;


          The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.




        7. The `$` sign missing in front of variable names



          Variables in PHP are represented by a dollar sign followed by the name of the variable.



          The dollar sign ($) is a sigil that marks the identifier as a name of a variable. Without this sigil, the identifier could be a language keyword or a constant.



          This is a common error when the PHP code was "translated" from code written in another language (C, Java, JavaScript, etc.). In such cases, a declaration of the variable type (when the original code was written in a language that uses typed variables) could also sneak out and produce this error.




        8. Escaped Quotation marks



          If you use in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.



          Example: echo 'Jim said 'Hello''; will print Jim said 'hello'



          If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.



          Very common error when specifiying paths in Windows: "C:xampphtdocs" is wrong. You need "C:\xampp\htdocs\".








        share|improve this answer

































          15














          Unexpected (



          Opening parentheses typically follow language constructs such as if/foreach/for/array/list or start an arithmetic expression. They're syntactically incorrect after "strings", a previous (), a lone $, and in some typical declaration contexts.





          1. Function declaration parameters



            A rarer occurrence for this error is trying to use expressions as default function parameters. This is not supported, even in PHP7:



            function header_fallback($value, $expires = time() + 90000) {


            Parameters in a function declaration can only be literal values or constant expressions. Unlike for function invocations, where you can freely use whatever(1+something()*2) etc.




          2. Class property defaults



            Same thing for class member declarations, where only literal/constant values are allowed, not expressions:



            class xyz {                   ⇓
            var $default = get_config("xyz_default");


            Put such things in the constructor.
            See also Why don't PHP attributes allow functions?



            Again note that PHP 7 only allows var $xy = 1 + 2 +3; constant expressions there.




          3. JavaScript syntax in PHP



            Using JavaScript or jQuery syntax won't work in PHP for obvious reasons:



            <?php      ⇓
            print $(document).text();


            When this happens, it usually indicates an unterminated preceding string; and literal <script> sections leaking into PHP code context.




          4. isset(()), empty, key, next, current



            Both isset() and empty() are language built-ins, not functions. They need to access a variable directly. If you inadvertently add a pair of parentheses too much, then you'd create an expression however:




            if (isset(($_GET["id"]))) {


            The same applies to any language construct that requires implicit variable name access. These built-ins are part of the language grammar, therefore don't permit decorative extra parentheses.



            User-level functions that require a variable reference -but get an expression result passed- lead to runtime errors instead.







          Unexpected )





          1. Absent function parameter



            You cannot have stray commas last in a function call. PHP expects a value there and thusly complains about an early closing ) parenthesis.




            callfunc(1, 2, );


            A trailing comma is only allowed in array() or list() constructs.




          2. Unfinished expressions



            If you forget something in an arithmetic expression, then the parser gives up. Because how should it possibly interpret that:




            $var = 2 * (1 + );


            And if you forgot the closing ) even, then you'd get a complaint about the unexpected semicolon instead.




          3. Foreach as constant



            For forgotten variable $ prefixes in control statements you will see:



                               ↓    ⇓
            foreach ($array as wrong) {


            PHP here sometimes tells you it expected a :: instead. Because a class::$variable could have satisfied the expected $variable expression..







          Unexpected {



          Curly braces { and } enclose code blocks. And syntax errors about them usually indicate some incorrec nesting.





          1. Unmatched subexpressions in an if



            Most commonly unbalanced ( and ) are the cause if the parser complains about the opening curly { appearing too early. A simple example:




            if (($x == $y) && (2 == true) {


            Count your parens or use an IDE which helps with that. Also don't write code without any spaces. Readability counts.




          2. { and } in expression context



            You can't use curly braces in expressions. If you confuse parentheses and curlys, it won't comply to the language grammer:




            $var = 5 * {7 + $x};


            There are a few exceptions for identifier construction, such as local scope variable ${references}.




          3. Variable variables or curly var expressions



            This is pretty rare. But you might also get { and } parser complaints for complex variable expressions:




            print "Hello {$world[2{]} !";


            Though there's a higher likelihood for an unexpected } in such contexts.







          Unexpected }



          When getting an "unexpected }" error, you've mostly closed a code block too early.





          1. Last statement in a code block



            It can happen for any unterminated expression.



            And if the last line in a function/code block lacks a trailing ; semicolon:



            function whatever() {
            doStuff()
            } ⇧


            Here the parser can't tell if you perhaps still wanted to add + 25; to the function result or something else.




          2. Invalid block nesting / Forgotten {



            You'll sometimes see this parser error when a code block was } closed too early, or you forgot an opening { even:



            function doStuff() {
            if (true) ⇦
            print "yes";
            }
            } ⇧


            In above snippet the if didn't have an opening { curly brace. Thus the closing } one below became redundant. And therefore the next closing }, which was intended for the function, was not associatable to the original opening { curly brace.



            Such errors are even harder to find without proper code indentation. Use an IDE and bracket matching.







          Unexpected {, expecting (



          Language constructs which require a condition/declaration header and a code block will trigger this error.





          1. Parameter lists



            For example misdeclared functions without parameter list are not permitted:




            function whatever {
            }



          2. Control statement conditions



            And you can't likewise have an if without condition.




            if {
            }


            Which doesn't make sense, obviously. The same thing for the usual suspects, for/foreach, while/do, etc.




            If you've got this particular error, you definitely should look up some manual examples.









          share|improve this answer





















          • 1





            Was looking for answer to my question in this post, but found answer myself to the problem of - "Unexpected {", that`s why i wanted to share with my answer- for me the problem was line breaking encoding - somehow some of my files were using macintosh line breaks, but when i changed them to windows line breaks - my problem(on localhost(WAMP) everything works, but on linux webserver dont) was solved.

            – Edgars Aivars
            Aug 21 '17 at 20:36













          • @EdgarsAivars Thanks for your comment! Platform-specific linebreaks are indeed an uncommon and tricky issue. I'll probably mention it within here as well. (It was just mentioned as aside in the other reference answer.)

            – mario
            Aug 21 '17 at 21:19











          • I found that getting Unexpected } was because a piece of my code used the php short tag <? instead of <?php - took me a while to find this one as it worked on other servers.

            – c7borg
            Sep 28 '18 at 12:00



















          14














          Unexpected $end



          When PHP talks about an "unexpected $end", it means that your code ended prematurely. (The message is a bit misleading when taken literally. It's not about a variable named "$end", as sometimes assumed by newcomers. It refers to the "end of file", EOF.)




          Cause: Unbalanced { and } for code blocks / and function or class declarations.




          It's pretty much always about a missing } curly brace to close preceding code blocks.




          • Again, use proper indentation to avoid such issues.



          • Use an IDE with bracket matching, to find out where the } is amiss.
            There are keyboard shortcuts in most IDEs and text editors:




            • NetBeans, PhpStorm, Komodo: Ctrl[ and Ctrl]

            • Eclipse, Aptana: CtrlShiftP

            • Atom, Sublime: Ctrlm - Zend Studio CtrlM

            • Geany, Notepad++: CtrlB - Joe: CtrlG - Emacs: C-M-n - Vim: %





          Most IDEs also highlight matching braces, brackets and parentheses.
          Which makes it pretty easy to inspect their correlation:



          Bracket matching in an IDE



          Unterminated expressions



          And Unexpected $end syntax/parser error can also occur for unterminated expressions or statements:





          • $var = func(1,
            ?>EOF


          So, look at the end of scripts first. A trailing ; is often redundant for the last statement in any PHP script. But you should have one. Precisely because it narrows such syntax issues down.



          Indented HEREDOC markers



          Another common occurrence appears with HEREDOC or NOWDOC strings. The terminating marker goes ignored with leading spaces, tabs, etc.:



          print <<< END
          Content...
          Content....
          END;
          # ↑ terminator isn't exactly at the line start


          Therefore the parser assumes the HEREDOC string to continue until the end of the file (hence "Unexpected $end"). Pretty much all IDEs and syntax-highlighting editors will make this obvious or warn about it.



          Escaped Quotation marks



          If you use in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.



          Example: echo 'Jim said 'Hello''; will print Jim said 'hello'



          If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.



          Very common error when specifiying paths in Windows: "C:xampphtdocs" is wrong. You need "C:\xampp\htdocs\".



          Alternative syntax



          Somewhat rarer you can see this syntax error when using the alternative syntax for statement/code blocks in templates. Using if: and else: and a missing endif; for example.



          See also:




          • PHP syntax error “unexpected $end”

          • Parse error: Syntax error, unexpected end of file in my PHP code

          • Parse error syntax error unexpected end of file, using PHP

          • PHP Parse error: syntax error, unexpected end of file in a CodeIgniter View

          • Parse error: syntax error, unexpected end of file (Registration script)

          • "Parse error: syntax error, unexpected $end" For my uni registration assignment

          • Fixing PHP Errors: PHP Error #3: Unexpected end of file






          share|improve this answer

































            13















            Unexpected T_IF

            Unexpected T_ELSEIF

            Unexpected T_ELSE

            Unexpected T_ENDIF



            Conditional control blocks if, elseif and else follow a simple structure. When you encounter a syntax error, it's most likely just invalid block nesting → with missing { curly braces } - or one too many.



            enter image description here





            1. Missing { or } due to incorrect indentation



              Mismatched code braces are common to less well-formatted code such as:



              if((!($opt["uniQartz5.8"]!=$this->check58)) or (empty($_POST['poree']))) {if
              ($true) {echo"halp";} elseif((!$z)or%b){excSmthng(False,5.8)}elseif (False){


              If your code looks like this, start afresh! Otherwise it's unfixable to you or anyone else. There's no point in showcasing this on the internet to inquire for help.



              You will only be able to fix it, if you can visually follow the nested structure and relation of if/else conditionals and their { code blocks }. Use your IDE to see if they're all paired.



              if (true) {
              if (false) {

              }
              elseif ($whatever) {
              if ($something2) {

              }
              else {

              }
              }
              else {

              }
              if (false) { // a second `if` tree

              }
              else {

              }
              }
              elseif (false) {

              }


              Any double } } will not just close a branch, but a previous condition structure. Therefore stick with one coding style; don't mix and match in nested if/else trees.



              Apart from consistency here, it turns out helpful to avoid lengthy conditions too. Use temporary variables or functions to avoid unreadable if-expressions.




            2. IF cannot be used in expressions



              A surprisingly frequent newcomer mistake is trying to use an if statement in an expression, such as a print statement:




              echo "<a href='" . if ($link == "example.org") { echo …


              Which is invalid of course.



              You can use a ternary conditional, but beware of readability impacts.



              echo "<a href='" . ($link ? "http://yes" : "http://no") . "</a>";


              Otherwise break such output constructs up: use multiple ifs and echos.

              Better yet, use temporary variables, and place your conditionals before:



              if ($link) { $href = "yes"; } else { $href = "no"; }
              echo "<a href='$href'>Link</a>";


              Defining functions or methods for such cases often makes sense too.



              Control blocks don't return "results"



              Now this is less common, but a few coders even try to treat if as if it could return a result:



              $var = if ($x == $y) { "true" };


              Which is structurally identical to using if within a string concatenation / expression.




              • But control structures (if / foreach / while) don't have a "result".

              • The literal string "true" would also just be a void statement.





              You'll have to use an assignment in the code block:



              if ($x == $y) { $var = "true"; }


              Alternatively, resort to a ?: ternary comparison.



              If in If



              You cannot nest an if within a condition either:




              if ($x == true and (if $y != false)) { ... }


              Which is obviously redundant, because the and (or or) already allows chaining comparisons.




            3. Forgotton ; semicolons



              Once more: Each control block needs to be a statement. If the previous code piece isn't terminated by a semicolon, then that's a guaranteed syntax error:




              $var = 1 + 2 + 3
              if (true) { … }


              Btw, the last line in a {…} code block needs a semicolon too.




            4. Semicolon too early



              Now it's probably wrong to blame a particular coding style, as this pitfall is too easy to overlook:




              if ($x == 5);
              {
              $y = 7;
              }
              else ←
              {
              $x = -1;
              }


              Which happens more often than you might imagine.




              • When you terminate the if () expression with ; it will execute a void statement. The ; becomes a an empty {} of its own!

              • The {…} block thus is detached from the if, and would always run.

              • So the else no longer had a relation to an open if construct,
                which is why this would lead to an Unexpected T_ELSE syntax error.





              Which also explains a likewise subtle variation of this syntax error:



              if ($x) { x_is_true(); }; else { something_else(); };


              Where the ; after the code block {…} terminates the whole if
              construct, severing the else branch syntactically.




            5. Not using code blocks



              It's syntactically allowed to omit curly braces {} for code blocks in if/elseif/else branches. Which sadly is a syntax style very common to unversed coders. (Under the false assumption this was quicker to type or read).



              However that's highly likely to trip up the syntax. Sooner or later additional statements will find their way into the if/else branches:



              if (true)
              $x = 5;
              elseif (false)
              $x = 6;
              $y = 7; ←
              else
              $z = 0;


              But to actually use code blocks, you do have to write {} them as such!




              Even seasoned programmers avoid this braceless syntax, or at least
              understand it as an exceptional exception to the rule.





            6. Else / Elseif in wrong order



              One thing to remind yourself is the conditional order, of course.



              if ($a) { … }
              else { … }
              elseif ($b) { … }



              You can have as many elseifs as you want, but else has to go last. That's just how it is.




            7. Class declarations



              As mentioned above, you can't have control statements in a class declaration:



              class xyz {
              if (true) {
              function ($var) {}
              }


              You either forgot a function definition, or closed one } too early in such cases.




            8. Unexpected T_ELSEIF / T_ELSE



              When mixing PHP and HTML, the closing } for an if/elseif must be in the same PHP block <?php ?> as the next elseif/else. This will generate an error as the closing } for the if needs to be part of the elseif:



              <?php if ($x) { ?>
              html
              <?php } ?>
              <?php elseif ($y) { ?>
              html
              <?php } ?>


              The correct form <?php } elseif:



              <?php if ($x) { ?>
              html
              <?php } elseif ($y) { ?>
              html
              <?php } ?>


              This is more or less a variation of incorrect indentation - presumably often based on wrong coding intentions.

              You cannot mash other statements inbetween if and elseif/else structural tokens:



              if (true) {
              }
              echo "in between"; ←
              elseif (false) {
              }
              ?> text <?php ←
              else {
              }


              Either can only occur in {…} code blocks, not in between control structure tokens.




              • This wouldn't make sense anyway. It's not like that there was some "undefined" state when PHP jumps between if and else branches.

              • You'll have to make up your mind where print statements belong to / or if they need to be repeated in both branches.





              Nor can you part an if/else between different control structures:



              foreach ($array as $i) {
              if ($i) { … }
              }
              else { … }


              There is no syntactic relation between the if and else. The foreach lexical scope ends at }, so there's no point for the if structure to continue.




            9. T_ENDIF



              If an unexpected T_ENDIF is complained about, you're using the alternative syntax style if:elseif:else:endif;. Which you should really think twice about.




              • A common pitfall is confusing the eerily similar : colon for a ; semicolon. (Covered in "Semicolon too early")


              • As indentation is harder to track in template files, the more when using the alternative syntax - it's plausible your endif; does not match any if:.


              • Using } endif;
                is a doubled if-terminator.




              While an "unexpected $end" is usually the price for a forgotten closing } curly brace.




            10. Assignment vs. comparison



              So, this is not a syntax error, but worth mentioning in this context:




              if ($x = true) { }
              else { do_false(); }


              That's not a ==/=== comparison, but an = assignment. This is rather subtle, and will easily lead some users to helplessly edit whole condition blocks. Watch out for unintended assignments first - whenver you experience a logic fault / misbeheviour.








            share|improve this answer

































              11















              Unexpected T_IF

              Unexpected T_FOREACH

              Unexpected T_FOR

              Unexpected T_WHILE

              Unexpected T_DO

              Unexpected T_ECHO



              Control constructs such as if, foreach, for, while, list, global, return, do, print, echo may only be used as statements. They usually reside on a line by themselves.





              1. Semicolon; where you at?



                Pretty universally have you missed a semicolon in the previous line if the parser complains about a control statement:




                $x = myfunc()
                if (true) {


                Solution: look into the previous line; add semicolon.




              2. Class declarations



                Another location where this occurs is in class declarations. In the class section you can only list property initializations and method sections. No code may reside there.



                class xyz {
                if (true) {}
                foreach ($var) {}


                Such syntax errors commonly materialize for incorrectly nested { and }. In particular when function code blocks got closed too early.




              3. Statements in expression context



                Most language constructs can only be used as statements. They aren't meant to be placed inside other expressions:




                $var = array(1, 2, foreach($else as $_), 5, 6);


                Likewise can't you use an if in strings, math expressions or elsewhere:




                print "Oh, " . if (true) { "you!" } . " won't work";
                // Use a ternary condition here instead, when versed enough.


                For embedding if-like conditions in an expression specifically, you often want to use a ?: ternary evaluation.



                The same applies to for, while, global, echo and a lesser extend list.




                echo 123, echo 567, "huh?";


                Whereas print() is a language builtin that may be used in expression context. (But rarely makes sense.)




              4. Reserved keywords as identifiers



                You also can't use do or if and other language constructs for user-defined functions or class names. (Perhaps in PHP7. But even then it wouldn't be advisable.)








              share|improve this answer

































                10















                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 >



                Comparison operators such as ==, >=, ===, !=, <>, !== and <= or < and > mostly should be used just in expressions, such as if expressions. If the parser complains about them, then it often means incorrect paring or mismatched ( ) parens around them.





                1. Parens grouping



                  In particular for if statements with multiple comparisons you must take care to correctly count opening and closing parenthesis:




                  if (($foo < 7) && $bar) > 5 || $baz < 9) { ... }



                  Here the if condition here was already terminated by the )



                  Once your comparisons become sufficiently complex it often helps to split it up into multiple and nested if constructs rather.




                2. isset() mashed with comparing



                  A common newcomer is pitfal is trying to combine isset() or empty() with comparisons:




                  if (empty($_POST["var"] == 1)) {


                  Or even:




                  if (isset($variable !== "value")) {


                  This doesn't make sense to PHP, because isset and empty are language constructs that only accept variable names. It doesn't make sense to compare the result either, because the output is only/already a boolean.




                3. Confusing >= greater-or-equal with => array operator



                  Both operators look somewhat similar, so they sometimes get mixed up:




                  if ($var => 5) { ... }


                  You only need to remember that this comparison operator is called "greater than or equal" to get it right.



                  See also: If statement structure in PHP




                4. Nothing to compare against



                  You also can't combine two comparisons if they pertain the same variable name:




                  if ($xyz > 5 and < 100)


                  PHP can't deduce that you meant to compare the initial variable again. Expressions are usually paired according to operator precedence, so by the time the < is seen, there'd be only a boolean result left from the original variable.



                  See also: unexpected T_IS_SMALLER_OR_EQUAL




                5. Comparison chains



                  You can't compare against a variable with a row of operators:




                  $reult = (5 < $x < 10);


                  This has to be broken up into two comparisons, each against $x.



                  This is actually more a case of blacklisted expressions (due to equivalent operator associativity). It's syntactically valid in a few C-style languages, but PHP wouldn't interpret it as expected comparison chain either.




                6. Unexpected >
                  Unexpected <



                  The greater than > or less than < operators don't have a custom T_XXX tokenizer name. And while they can be misplaced like all they others, you more often see the parser complain about them for misquoted strings and mashed HTML:




                  print "<a href='z">Hello</a>";



                  This amounts to a string "<a href='z" being compared > to a literal constant Hello and then another < comparison. Or that's at least how PHP sees it. The actual cause and syntax mistake was the premature string " termination.



                  It's also not possible to nest PHP start tags:



                  <?php echo <?php my_func(); ?>




                See also:




                • php unexpected T_IS_NOT_EQUAL error

                • syntax error, unexpected T_IS_EQUAL

                • Syntax error on return statement

                • http://forums.phpfreaks.com/topic/96891-parse-error-syntax-error-unexpected-t-is-not-identical-expecting-or/






                share|improve this answer

































                  5














                  Unexpected T_LNUMBER



                  The token T_LNUMBER refers to a "long" / number.





                  1. Invalid variable names



                    In PHP, and most other programming languages, variables cannot start with a number. The first character must be alphabetic or an underscore.



                    $1   // Bad
                    $_1 // Good


                    *





                    • Quite often comes up for using preg_replace-placeholders "$1" in PHP context:



                      #                         ↓            ⇓  ↓
                      preg_replace("/#(w+)/e", strtopupper($1) )


                      Where the callback should have been quoted. (Now the /e regex flag has been deprecated. But it's sometimes still misused in preg_replace_callback functions.)




                    • The same identifier constraint applies to object properties, btw.




                      $json->0->value


                    • While the tokenizer/parser does not allow a literal $1 as variable name, one could use ${1} or ${"1"}. Which is a syntactic workaround for non-standard identifiers. (It's best to think of it as a local scope lookup. But generally: prefer plain arrays for such cases!)


                    • Amusingly, but very much not recommended, PHPs parser allows Unicode-identifiers; such that $➊ would be valid. (Unlike a literal 1).





                  2. Stray array entry



                    An unexpected long can also occur for array declarations - when missing , commas:



                    #            ↓ ↓
                    $xy = array(1 2 3);


                    Or likewise function calls and declarations, and other constructs:




                    • func(1, 2 3);

                    • function xy($z 2);


                    • for ($i=2 3<$z)




                    So usually there's one of ; or , missing for separating lists or expressions.




                  3. Misquoted HTML



                    And again, misquoted strings are a frequent source of stray numbers:



                    #                 ↓ ↓          
                    echo "<td colspan="3">something bad</td>";


                    Such cases should be treated more or less like Unexpected T_STRING errors.




                  4. Other identifiers



                    Neither functions, classes, nor namespaces can be named beginning with a number either:




                    function 123shop() {


                    Pretty much the same as for variable names.








                  share|improve this answer

































                    5














                    Unexpected '?'



                    If you are trying to use the null coalescing operator ?? in a version of PHP prior to PHP 7 you will get this error.



                    <?= $a ?? 2; // works in PHP 7+
                    <?= (!empty($a)) ? $a : 2; // All versions of PHP


                    Unexpected '?', expecting variable



                    A similar error can occur for nullable types, as in:



                    function add(?int $sum): ?int {


                    Which again indicates an outdated PHP version being used (either the CLI version php -v or the webserver bound one phpinfo();).






                    share|improve this answer

































                      2














                      Unexpected '='



                      This can be caused by having invalid characters in a variable name. Variables names must follow these rules:




                      Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*'







                      share|improve this answer


























                      • Good addition John.

                        – Funk Forty Niner
                        Dec 19 '18 at 3:02



















                      1














                      Unexpected 'continue' (T_CONTINUE)



                      continue is a statement (like for, or if) and must appear standalone. It cannot be used as part of an expression. Partly because continue doesn't return a value, but in an expression every sub-expression must result in some value so the overall expression results in a value. That's the difference between a statement and an expression.



                      That means continue cannot be used in a ternary statement or any statement that requires a return value.



                      Unexpected 'break' (T_BREAK)



                      Same goes for break; of course. It's also not usable in expression context, but a strict statement (on the same level as foreach or an if block).



                      Unexpected 'return' (T_RETURN)



                      Now this might be more surprising for return, but that's also just a block-level statement. It does return a value (or NULL) to the higher scope/function, but it does not evaluate as expression itself. → That is: there's no point in doing return(return(false);;






                      share|improve this answer

































                        15 Answers
                        15






                        active

                        oldest

                        votes








                        15 Answers
                        15






                        active

                        oldest

                        votes









                        active

                        oldest

                        votes






                        active

                        oldest

                        votes









                        247














                        What are the syntax errors?



                        PHP belongs to the C-style and imperative programming languages. It has rigid grammar rules, which it cannot recover from when encountering misplaced symbols or identifiers. It can't guess your coding intentions.



                        Function definition syntax abstract



                        Most important tips



                        There are a few basic precautions you can always take:




                        • Use proper code indentation, or adopt any lofty coding style.
                          Readability prevents irregularities.



                        • Use an IDE or editor for PHP with syntax highlighting.
                          Which also help with parentheses/bracket balancing.



                          Expected: semicolon



                        • Read the language reference and examples in the manual.
                          Twice, to become somewhat proficient.



                        How to interpret parser errors



                        A typical syntax error message reads:




                        Parse error: syntax error, unexpected T_STRING, expecting ';' in file.php on line 217




                        Which lists the possible location of a syntax mistake. See the mentioned file name and line number.



                        A moniker such as T_STRING explains which symbol the parser/tokenizer couldn't process finally. This isn't necessarily the cause of the syntax mistake, however.



                        It's important to look into previous code lines as well. Often syntax errors are just mishaps that happened earlier. The error line number is just where the parser conclusively gave up to process it all.



                        Solving syntax errors



                        There are many approaches to narrow down and fix syntax hiccups.





                        • Open the mentioned source file. Look at the mentioned code line.




                          • For runaway strings and misplaced operators, this is usually where you find the culprit.


                          • Read the line left to right and imagine what each symbol does.





                        • More regularly you need to look at preceding lines as well.




                          • In particular, missing ; semicolons are missing at the previous line ends/statement. (At least from the stylistic viewpoint. )


                          • If { code blocks } are incorrectly closed or nested, you may need to investigate even further up the source code. Use proper code indentation to simplify that.





                        • Look at the syntax colorization!




                          • Strings and variables and constants should all have different colors.


                          • Operators +-*/. should be tinted distinct as well. Else they might be in the wrong context.


                          • If you see string colorization extend too far or too short, then you have found an unescaped or missing closing " or ' string marker.


                          • Having two same-colored punctuation characters next to each other can also mean trouble. Usually, operators are lone if it's not ++, --, or parentheses following an operator. Two strings/identifiers directly following each other are incorrect in most contexts.




                        • Whitespace is your friend.
                          Follow any coding style.



                        • Break up long lines temporarily.




                          • You can freely add newlines between operators or constants and strings. The parser will then concretize the line number for parsing errors. Instead of looking at the very lengthy code, you can isolate the missing or misplaced syntax symbol.


                          • Split up complex if statements into distinct or nested if conditions.


                          • Instead of lengthy math formulas or logic chains, use temporary variables to simplify the code. (More readable = fewer errors.)



                          • Add newlines between:




                            1. The code you can easily identify as correct,

                            2. The parts you're unsure about,

                            3. And the lines which the parser complains about.



                            Partitioning up long code blocks really helps to locate the origin of syntax errors.






                        • Comment out offending code.




                          • If you can't isolate the problem source, start to comment out (and thus temporarily remove) blocks of code.


                          • As soon as you got rid of the parsing error, you have found the problem source. Look more closely there.


                          • Sometimes you want to temporarily remove complete function/method blocks. (In case of unmatched curly braces and wrongly indented code.)


                          • When you can't resolve the syntax issue, try to rewrite the commented out sections from scratch.





                        • As a newcomer, avoid some of the confusing syntax constructs.




                          • The ternary ? : condition operator can compact code and is useful indeed. But it doesn't aid readability in all cases. Prefer plain if statements while unversed.


                          • PHP's alternative syntax (if:/elseif:/endif;) is common for templates, but arguably less easy to follow than normal { code } blocks.





                        • The most prevalent newcomer mistakes are:




                          • Missing semicolons ; for terminating statements/lines.


                          • Mismatched string quotes for " or ' and unescaped quotes within.


                          • Forgotten operators, in particular for the string . concatenation.


                          • Unbalanced ( parentheses ). Count them in the reported line. Are there an equal number of them?





                        • Don't forget that solving one syntax problem can uncover the next.




                          • If you make one issue go away, but other crops up in some code below, you're mostly on the right path.


                          • If after editing a new syntax error crops up in the same line, then your attempted change was possibly a failure. (Not always though.)





                        • Restore a backup of previously working code, if you can't fix it.




                          • Adopt a source code versioning system. You can always view a diff of the broken and last working version. Which might be enlightening as to what the syntax problem is.






                        • Invisible stray Unicode characters: In some cases, you need to use a hexeditor or different editor/viewer on your source. Some problems cannot be found just from looking at your code.




                          • Try grep --color -P -n "[x80-xFF]" file.php as the first measure to find non-ASCII symbols.


                          • In particular BOMs, zero-width spaces, or non-breaking spaces, and smart quotes regularly can find their way into the source code.





                        • Take care of which type of linebreaks are saved in files.




                          • PHP just honors n newlines, not r carriage returns.


                          • Which is occasionally an issue for MacOS users (even on OS  X for misconfigured editors).


                          • It often only surfaces as an issue when single-line // or # comments are used. Multiline /*...*/ comments do seldom disturb the parser when linebreaks get ignored.





                        • If your syntax error does not transmit over the web:
                          It happens that you have a syntax error on your machine. But posting the very same file online does not exhibit it anymore. Which can only mean one of two things:




                          • You are looking at the wrong file!


                          • Or your code contained invisible stray Unicode (see above).
                            You can easily find out: Just copy your code back from the web form into your text editor.





                        • Check your PHP version. Not all syntax constructs are available on every server.




                          • php -v for the command line interpreter


                          • <?php phpinfo(); for the one invoked through the webserver.




                          Those aren't necessarily the same. In particular when working with frameworks, you will them to match up.



                        • Don't use PHP's reserved keywords as identifiers for functions/methods, classes or constants.


                        • Trial-and-error is your last resort.



                        If all else fails, you can always google your error message. Syntax symbols aren't as easy to search for (Stack Overflow itself is indexed by SymbolHound though). Therefore it may take looking through a few more pages before you find something relevant.



                        Further guides:





                        • PHP Debugging Basics by David Sklar


                        • Fixing PHP Errors by Jason McCreary


                        • PHP Errors – 10 Common Mistakes by Mario Lurig

                        • Common PHP Errors and Solutions

                        • How to Troubleshoot and Fix your WordPress Website


                        • A Guide To PHP Error Messages For Designers - Smashing Magazine


                        White screen of death



                        If your website is just blank, then typically a syntax error is the cause.
                        Enable their display with:




                        • error_reporting = E_ALL

                        • display_errors = 1


                        In your php.ini generally, or via .htaccess for mod_php,
                        or even .user.ini with FastCGI setups.



                        Enabling it within the broken script is too late because PHP can't even interpret/run the first line. A quick workaround is crafting a wrapper script, say test.php:



                        <?php
                        error_reporting(E_ALL);
                        ini_set("display_errors", 1);
                        include("./broken-script.php");


                        Then invoke the failing code by accessing this wrapper script.



                        It also helps to enable PHP's error_log and look into your webserver's error.log when a script crashes with HTTP 500 responses.






                        share|improve this answer


























                        • error_reporting(E_ALL | E_STRICT); for earlier versions of PHP

                          – Geo
                          Aug 15 '13 at 21:42






                        • 2





                          Some IDEs (like NetBeans) not only support syntax highlighting but also code formatting. If you get into the habit of formatting your code properly and asking the IDE to reformat just in case from time to time you may catch hard to spot problems like unmatched braces.

                          – Josep Valls
                          May 21 '15 at 19:20
















                        247














                        What are the syntax errors?



                        PHP belongs to the C-style and imperative programming languages. It has rigid grammar rules, which it cannot recover from when encountering misplaced symbols or identifiers. It can't guess your coding intentions.



                        Function definition syntax abstract



                        Most important tips



                        There are a few basic precautions you can always take:




                        • Use proper code indentation, or adopt any lofty coding style.
                          Readability prevents irregularities.



                        • Use an IDE or editor for PHP with syntax highlighting.
                          Which also help with parentheses/bracket balancing.



                          Expected: semicolon



                        • Read the language reference and examples in the manual.
                          Twice, to become somewhat proficient.



                        How to interpret parser errors



                        A typical syntax error message reads:




                        Parse error: syntax error, unexpected T_STRING, expecting ';' in file.php on line 217




                        Which lists the possible location of a syntax mistake. See the mentioned file name and line number.



                        A moniker such as T_STRING explains which symbol the parser/tokenizer couldn't process finally. This isn't necessarily the cause of the syntax mistake, however.



                        It's important to look into previous code lines as well. Often syntax errors are just mishaps that happened earlier. The error line number is just where the parser conclusively gave up to process it all.



                        Solving syntax errors



                        There are many approaches to narrow down and fix syntax hiccups.





                        • Open the mentioned source file. Look at the mentioned code line.




                          • For runaway strings and misplaced operators, this is usually where you find the culprit.


                          • Read the line left to right and imagine what each symbol does.





                        • More regularly you need to look at preceding lines as well.




                          • In particular, missing ; semicolons are missing at the previous line ends/statement. (At least from the stylistic viewpoint. )


                          • If { code blocks } are incorrectly closed or nested, you may need to investigate even further up the source code. Use proper code indentation to simplify that.





                        • Look at the syntax colorization!




                          • Strings and variables and constants should all have different colors.


                          • Operators +-*/. should be tinted distinct as well. Else they might be in the wrong context.


                          • If you see string colorization extend too far or too short, then you have found an unescaped or missing closing " or ' string marker.


                          • Having two same-colored punctuation characters next to each other can also mean trouble. Usually, operators are lone if it's not ++, --, or parentheses following an operator. Two strings/identifiers directly following each other are incorrect in most contexts.




                        • Whitespace is your friend.
                          Follow any coding style.



                        • Break up long lines temporarily.




                          • You can freely add newlines between operators or constants and strings. The parser will then concretize the line number for parsing errors. Instead of looking at the very lengthy code, you can isolate the missing or misplaced syntax symbol.


                          • Split up complex if statements into distinct or nested if conditions.


                          • Instead of lengthy math formulas or logic chains, use temporary variables to simplify the code. (More readable = fewer errors.)



                          • Add newlines between:




                            1. The code you can easily identify as correct,

                            2. The parts you're unsure about,

                            3. And the lines which the parser complains about.



                            Partitioning up long code blocks really helps to locate the origin of syntax errors.






                        • Comment out offending code.




                          • If you can't isolate the problem source, start to comment out (and thus temporarily remove) blocks of code.


                          • As soon as you got rid of the parsing error, you have found the problem source. Look more closely there.


                          • Sometimes you want to temporarily remove complete function/method blocks. (In case of unmatched curly braces and wrongly indented code.)


                          • When you can't resolve the syntax issue, try to rewrite the commented out sections from scratch.





                        • As a newcomer, avoid some of the confusing syntax constructs.




                          • The ternary ? : condition operator can compact code and is useful indeed. But it doesn't aid readability in all cases. Prefer plain if statements while unversed.


                          • PHP's alternative syntax (if:/elseif:/endif;) is common for templates, but arguably less easy to follow than normal { code } blocks.





                        • The most prevalent newcomer mistakes are:




                          • Missing semicolons ; for terminating statements/lines.


                          • Mismatched string quotes for " or ' and unescaped quotes within.


                          • Forgotten operators, in particular for the string . concatenation.


                          • Unbalanced ( parentheses ). Count them in the reported line. Are there an equal number of them?





                        • Don't forget that solving one syntax problem can uncover the next.




                          • If you make one issue go away, but other crops up in some code below, you're mostly on the right path.


                          • If after editing a new syntax error crops up in the same line, then your attempted change was possibly a failure. (Not always though.)





                        • Restore a backup of previously working code, if you can't fix it.




                          • Adopt a source code versioning system. You can always view a diff of the broken and last working version. Which might be enlightening as to what the syntax problem is.






                        • Invisible stray Unicode characters: In some cases, you need to use a hexeditor or different editor/viewer on your source. Some problems cannot be found just from looking at your code.




                          • Try grep --color -P -n "[x80-xFF]" file.php as the first measure to find non-ASCII symbols.


                          • In particular BOMs, zero-width spaces, or non-breaking spaces, and smart quotes regularly can find their way into the source code.





                        • Take care of which type of linebreaks are saved in files.




                          • PHP just honors n newlines, not r carriage returns.


                          • Which is occasionally an issue for MacOS users (even on OS  X for misconfigured editors).


                          • It often only surfaces as an issue when single-line // or # comments are used. Multiline /*...*/ comments do seldom disturb the parser when linebreaks get ignored.





                        • If your syntax error does not transmit over the web:
                          It happens that you have a syntax error on your machine. But posting the very same file online does not exhibit it anymore. Which can only mean one of two things:




                          • You are looking at the wrong file!


                          • Or your code contained invisible stray Unicode (see above).
                            You can easily find out: Just copy your code back from the web form into your text editor.





                        • Check your PHP version. Not all syntax constructs are available on every server.




                          • php -v for the command line interpreter


                          • <?php phpinfo(); for the one invoked through the webserver.




                          Those aren't necessarily the same. In particular when working with frameworks, you will them to match up.



                        • Don't use PHP's reserved keywords as identifiers for functions/methods, classes or constants.


                        • Trial-and-error is your last resort.



                        If all else fails, you can always google your error message. Syntax symbols aren't as easy to search for (Stack Overflow itself is indexed by SymbolHound though). Therefore it may take looking through a few more pages before you find something relevant.



                        Further guides:





                        • PHP Debugging Basics by David Sklar


                        • Fixing PHP Errors by Jason McCreary


                        • PHP Errors – 10 Common Mistakes by Mario Lurig

                        • Common PHP Errors and Solutions

                        • How to Troubleshoot and Fix your WordPress Website


                        • A Guide To PHP Error Messages For Designers - Smashing Magazine


                        White screen of death



                        If your website is just blank, then typically a syntax error is the cause.
                        Enable their display with:




                        • error_reporting = E_ALL

                        • display_errors = 1


                        In your php.ini generally, or via .htaccess for mod_php,
                        or even .user.ini with FastCGI setups.



                        Enabling it within the broken script is too late because PHP can't even interpret/run the first line. A quick workaround is crafting a wrapper script, say test.php:



                        <?php
                        error_reporting(E_ALL);
                        ini_set("display_errors", 1);
                        include("./broken-script.php");


                        Then invoke the failing code by accessing this wrapper script.



                        It also helps to enable PHP's error_log and look into your webserver's error.log when a script crashes with HTTP 500 responses.






                        share|improve this answer


























                        • error_reporting(E_ALL | E_STRICT); for earlier versions of PHP

                          – Geo
                          Aug 15 '13 at 21:42






                        • 2





                          Some IDEs (like NetBeans) not only support syntax highlighting but also code formatting. If you get into the habit of formatting your code properly and asking the IDE to reformat just in case from time to time you may catch hard to spot problems like unmatched braces.

                          – Josep Valls
                          May 21 '15 at 19:20














                        247












                        247








                        247







                        What are the syntax errors?



                        PHP belongs to the C-style and imperative programming languages. It has rigid grammar rules, which it cannot recover from when encountering misplaced symbols or identifiers. It can't guess your coding intentions.



                        Function definition syntax abstract



                        Most important tips



                        There are a few basic precautions you can always take:




                        • Use proper code indentation, or adopt any lofty coding style.
                          Readability prevents irregularities.



                        • Use an IDE or editor for PHP with syntax highlighting.
                          Which also help with parentheses/bracket balancing.



                          Expected: semicolon



                        • Read the language reference and examples in the manual.
                          Twice, to become somewhat proficient.



                        How to interpret parser errors



                        A typical syntax error message reads:




                        Parse error: syntax error, unexpected T_STRING, expecting ';' in file.php on line 217




                        Which lists the possible location of a syntax mistake. See the mentioned file name and line number.



                        A moniker such as T_STRING explains which symbol the parser/tokenizer couldn't process finally. This isn't necessarily the cause of the syntax mistake, however.



                        It's important to look into previous code lines as well. Often syntax errors are just mishaps that happened earlier. The error line number is just where the parser conclusively gave up to process it all.



                        Solving syntax errors



                        There are many approaches to narrow down and fix syntax hiccups.





                        • Open the mentioned source file. Look at the mentioned code line.




                          • For runaway strings and misplaced operators, this is usually where you find the culprit.


                          • Read the line left to right and imagine what each symbol does.





                        • More regularly you need to look at preceding lines as well.




                          • In particular, missing ; semicolons are missing at the previous line ends/statement. (At least from the stylistic viewpoint. )


                          • If { code blocks } are incorrectly closed or nested, you may need to investigate even further up the source code. Use proper code indentation to simplify that.





                        • Look at the syntax colorization!




                          • Strings and variables and constants should all have different colors.


                          • Operators +-*/. should be tinted distinct as well. Else they might be in the wrong context.


                          • If you see string colorization extend too far or too short, then you have found an unescaped or missing closing " or ' string marker.


                          • Having two same-colored punctuation characters next to each other can also mean trouble. Usually, operators are lone if it's not ++, --, or parentheses following an operator. Two strings/identifiers directly following each other are incorrect in most contexts.




                        • Whitespace is your friend.
                          Follow any coding style.



                        • Break up long lines temporarily.




                          • You can freely add newlines between operators or constants and strings. The parser will then concretize the line number for parsing errors. Instead of looking at the very lengthy code, you can isolate the missing or misplaced syntax symbol.


                          • Split up complex if statements into distinct or nested if conditions.


                          • Instead of lengthy math formulas or logic chains, use temporary variables to simplify the code. (More readable = fewer errors.)



                          • Add newlines between:




                            1. The code you can easily identify as correct,

                            2. The parts you're unsure about,

                            3. And the lines which the parser complains about.



                            Partitioning up long code blocks really helps to locate the origin of syntax errors.






                        • Comment out offending code.




                          • If you can't isolate the problem source, start to comment out (and thus temporarily remove) blocks of code.


                          • As soon as you got rid of the parsing error, you have found the problem source. Look more closely there.


                          • Sometimes you want to temporarily remove complete function/method blocks. (In case of unmatched curly braces and wrongly indented code.)


                          • When you can't resolve the syntax issue, try to rewrite the commented out sections from scratch.





                        • As a newcomer, avoid some of the confusing syntax constructs.




                          • The ternary ? : condition operator can compact code and is useful indeed. But it doesn't aid readability in all cases. Prefer plain if statements while unversed.


                          • PHP's alternative syntax (if:/elseif:/endif;) is common for templates, but arguably less easy to follow than normal { code } blocks.





                        • The most prevalent newcomer mistakes are:




                          • Missing semicolons ; for terminating statements/lines.


                          • Mismatched string quotes for " or ' and unescaped quotes within.


                          • Forgotten operators, in particular for the string . concatenation.


                          • Unbalanced ( parentheses ). Count them in the reported line. Are there an equal number of them?





                        • Don't forget that solving one syntax problem can uncover the next.




                          • If you make one issue go away, but other crops up in some code below, you're mostly on the right path.


                          • If after editing a new syntax error crops up in the same line, then your attempted change was possibly a failure. (Not always though.)





                        • Restore a backup of previously working code, if you can't fix it.




                          • Adopt a source code versioning system. You can always view a diff of the broken and last working version. Which might be enlightening as to what the syntax problem is.






                        • Invisible stray Unicode characters: In some cases, you need to use a hexeditor or different editor/viewer on your source. Some problems cannot be found just from looking at your code.




                          • Try grep --color -P -n "[x80-xFF]" file.php as the first measure to find non-ASCII symbols.


                          • In particular BOMs, zero-width spaces, or non-breaking spaces, and smart quotes regularly can find their way into the source code.





                        • Take care of which type of linebreaks are saved in files.




                          • PHP just honors n newlines, not r carriage returns.


                          • Which is occasionally an issue for MacOS users (even on OS  X for misconfigured editors).


                          • It often only surfaces as an issue when single-line // or # comments are used. Multiline /*...*/ comments do seldom disturb the parser when linebreaks get ignored.





                        • If your syntax error does not transmit over the web:
                          It happens that you have a syntax error on your machine. But posting the very same file online does not exhibit it anymore. Which can only mean one of two things:




                          • You are looking at the wrong file!


                          • Or your code contained invisible stray Unicode (see above).
                            You can easily find out: Just copy your code back from the web form into your text editor.





                        • Check your PHP version. Not all syntax constructs are available on every server.




                          • php -v for the command line interpreter


                          • <?php phpinfo(); for the one invoked through the webserver.




                          Those aren't necessarily the same. In particular when working with frameworks, you will them to match up.



                        • Don't use PHP's reserved keywords as identifiers for functions/methods, classes or constants.


                        • Trial-and-error is your last resort.



                        If all else fails, you can always google your error message. Syntax symbols aren't as easy to search for (Stack Overflow itself is indexed by SymbolHound though). Therefore it may take looking through a few more pages before you find something relevant.



                        Further guides:





                        • PHP Debugging Basics by David Sklar


                        • Fixing PHP Errors by Jason McCreary


                        • PHP Errors – 10 Common Mistakes by Mario Lurig

                        • Common PHP Errors and Solutions

                        • How to Troubleshoot and Fix your WordPress Website


                        • A Guide To PHP Error Messages For Designers - Smashing Magazine


                        White screen of death



                        If your website is just blank, then typically a syntax error is the cause.
                        Enable their display with:




                        • error_reporting = E_ALL

                        • display_errors = 1


                        In your php.ini generally, or via .htaccess for mod_php,
                        or even .user.ini with FastCGI setups.



                        Enabling it within the broken script is too late because PHP can't even interpret/run the first line. A quick workaround is crafting a wrapper script, say test.php:



                        <?php
                        error_reporting(E_ALL);
                        ini_set("display_errors", 1);
                        include("./broken-script.php");


                        Then invoke the failing code by accessing this wrapper script.



                        It also helps to enable PHP's error_log and look into your webserver's error.log when a script crashes with HTTP 500 responses.






                        share|improve this answer















                        What are the syntax errors?



                        PHP belongs to the C-style and imperative programming languages. It has rigid grammar rules, which it cannot recover from when encountering misplaced symbols or identifiers. It can't guess your coding intentions.



                        Function definition syntax abstract



                        Most important tips



                        There are a few basic precautions you can always take:




                        • Use proper code indentation, or adopt any lofty coding style.
                          Readability prevents irregularities.



                        • Use an IDE or editor for PHP with syntax highlighting.
                          Which also help with parentheses/bracket balancing.



                          Expected: semicolon



                        • Read the language reference and examples in the manual.
                          Twice, to become somewhat proficient.



                        How to interpret parser errors



                        A typical syntax error message reads:




                        Parse error: syntax error, unexpected T_STRING, expecting ';' in file.php on line 217




                        Which lists the possible location of a syntax mistake. See the mentioned file name and line number.



                        A moniker such as T_STRING explains which symbol the parser/tokenizer couldn't process finally. This isn't necessarily the cause of the syntax mistake, however.



                        It's important to look into previous code lines as well. Often syntax errors are just mishaps that happened earlier. The error line number is just where the parser conclusively gave up to process it all.



                        Solving syntax errors



                        There are many approaches to narrow down and fix syntax hiccups.





                        • Open the mentioned source file. Look at the mentioned code line.




                          • For runaway strings and misplaced operators, this is usually where you find the culprit.


                          • Read the line left to right and imagine what each symbol does.





                        • More regularly you need to look at preceding lines as well.




                          • In particular, missing ; semicolons are missing at the previous line ends/statement. (At least from the stylistic viewpoint. )


                          • If { code blocks } are incorrectly closed or nested, you may need to investigate even further up the source code. Use proper code indentation to simplify that.





                        • Look at the syntax colorization!




                          • Strings and variables and constants should all have different colors.


                          • Operators +-*/. should be tinted distinct as well. Else they might be in the wrong context.


                          • If you see string colorization extend too far or too short, then you have found an unescaped or missing closing " or ' string marker.


                          • Having two same-colored punctuation characters next to each other can also mean trouble. Usually, operators are lone if it's not ++, --, or parentheses following an operator. Two strings/identifiers directly following each other are incorrect in most contexts.




                        • Whitespace is your friend.
                          Follow any coding style.



                        • Break up long lines temporarily.




                          • You can freely add newlines between operators or constants and strings. The parser will then concretize the line number for parsing errors. Instead of looking at the very lengthy code, you can isolate the missing or misplaced syntax symbol.


                          • Split up complex if statements into distinct or nested if conditions.


                          • Instead of lengthy math formulas or logic chains, use temporary variables to simplify the code. (More readable = fewer errors.)



                          • Add newlines between:




                            1. The code you can easily identify as correct,

                            2. The parts you're unsure about,

                            3. And the lines which the parser complains about.



                            Partitioning up long code blocks really helps to locate the origin of syntax errors.






                        • Comment out offending code.




                          • If you can't isolate the problem source, start to comment out (and thus temporarily remove) blocks of code.


                          • As soon as you got rid of the parsing error, you have found the problem source. Look more closely there.


                          • Sometimes you want to temporarily remove complete function/method blocks. (In case of unmatched curly braces and wrongly indented code.)


                          • When you can't resolve the syntax issue, try to rewrite the commented out sections from scratch.





                        • As a newcomer, avoid some of the confusing syntax constructs.




                          • The ternary ? : condition operator can compact code and is useful indeed. But it doesn't aid readability in all cases. Prefer plain if statements while unversed.


                          • PHP's alternative syntax (if:/elseif:/endif;) is common for templates, but arguably less easy to follow than normal { code } blocks.





                        • The most prevalent newcomer mistakes are:




                          • Missing semicolons ; for terminating statements/lines.


                          • Mismatched string quotes for " or ' and unescaped quotes within.


                          • Forgotten operators, in particular for the string . concatenation.


                          • Unbalanced ( parentheses ). Count them in the reported line. Are there an equal number of them?





                        • Don't forget that solving one syntax problem can uncover the next.




                          • If you make one issue go away, but other crops up in some code below, you're mostly on the right path.


                          • If after editing a new syntax error crops up in the same line, then your attempted change was possibly a failure. (Not always though.)





                        • Restore a backup of previously working code, if you can't fix it.




                          • Adopt a source code versioning system. You can always view a diff of the broken and last working version. Which might be enlightening as to what the syntax problem is.






                        • Invisible stray Unicode characters: In some cases, you need to use a hexeditor or different editor/viewer on your source. Some problems cannot be found just from looking at your code.




                          • Try grep --color -P -n "[x80-xFF]" file.php as the first measure to find non-ASCII symbols.


                          • In particular BOMs, zero-width spaces, or non-breaking spaces, and smart quotes regularly can find their way into the source code.





                        • Take care of which type of linebreaks are saved in files.




                          • PHP just honors n newlines, not r carriage returns.


                          • Which is occasionally an issue for MacOS users (even on OS  X for misconfigured editors).


                          • It often only surfaces as an issue when single-line // or # comments are used. Multiline /*...*/ comments do seldom disturb the parser when linebreaks get ignored.





                        • If your syntax error does not transmit over the web:
                          It happens that you have a syntax error on your machine. But posting the very same file online does not exhibit it anymore. Which can only mean one of two things:




                          • You are looking at the wrong file!


                          • Or your code contained invisible stray Unicode (see above).
                            You can easily find out: Just copy your code back from the web form into your text editor.





                        • Check your PHP version. Not all syntax constructs are available on every server.




                          • php -v for the command line interpreter


                          • <?php phpinfo(); for the one invoked through the webserver.




                          Those aren't necessarily the same. In particular when working with frameworks, you will them to match up.



                        • Don't use PHP's reserved keywords as identifiers for functions/methods, classes or constants.


                        • Trial-and-error is your last resort.



                        If all else fails, you can always google your error message. Syntax symbols aren't as easy to search for (Stack Overflow itself is indexed by SymbolHound though). Therefore it may take looking through a few more pages before you find something relevant.



                        Further guides:





                        • PHP Debugging Basics by David Sklar


                        • Fixing PHP Errors by Jason McCreary


                        • PHP Errors – 10 Common Mistakes by Mario Lurig

                        • Common PHP Errors and Solutions

                        • How to Troubleshoot and Fix your WordPress Website


                        • A Guide To PHP Error Messages For Designers - Smashing Magazine


                        White screen of death



                        If your website is just blank, then typically a syntax error is the cause.
                        Enable their display with:




                        • error_reporting = E_ALL

                        • display_errors = 1


                        In your php.ini generally, or via .htaccess for mod_php,
                        or even .user.ini with FastCGI setups.



                        Enabling it within the broken script is too late because PHP can't even interpret/run the first line. A quick workaround is crafting a wrapper script, say test.php:



                        <?php
                        error_reporting(E_ALL);
                        ini_set("display_errors", 1);
                        include("./broken-script.php");


                        Then invoke the failing code by accessing this wrapper script.



                        It also helps to enable PHP's error_log and look into your webserver's error.log when a script crashes with HTTP 500 responses.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jan 9 at 14:13


























                        community wiki





                        13 revs, 4 users 86%
                        mario














                        • error_reporting(E_ALL | E_STRICT); for earlier versions of PHP

                          – Geo
                          Aug 15 '13 at 21:42






                        • 2





                          Some IDEs (like NetBeans) not only support syntax highlighting but also code formatting. If you get into the habit of formatting your code properly and asking the IDE to reformat just in case from time to time you may catch hard to spot problems like unmatched braces.

                          – Josep Valls
                          May 21 '15 at 19:20



















                        • error_reporting(E_ALL | E_STRICT); for earlier versions of PHP

                          – Geo
                          Aug 15 '13 at 21:42






                        • 2





                          Some IDEs (like NetBeans) not only support syntax highlighting but also code formatting. If you get into the habit of formatting your code properly and asking the IDE to reformat just in case from time to time you may catch hard to spot problems like unmatched braces.

                          – Josep Valls
                          May 21 '15 at 19:20

















                        error_reporting(E_ALL | E_STRICT); for earlier versions of PHP

                        – Geo
                        Aug 15 '13 at 21:42





                        error_reporting(E_ALL | E_STRICT); for earlier versions of PHP

                        – Geo
                        Aug 15 '13 at 21:42




                        2




                        2





                        Some IDEs (like NetBeans) not only support syntax highlighting but also code formatting. If you get into the habit of formatting your code properly and asking the IDE to reformat just in case from time to time you may catch hard to spot problems like unmatched braces.

                        – Josep Valls
                        May 21 '15 at 19:20





                        Some IDEs (like NetBeans) not only support syntax highlighting but also code formatting. If you get into the habit of formatting your code properly and asking the IDE to reformat just in case from time to time you may catch hard to spot problems like unmatched braces.

                        – Josep Valls
                        May 21 '15 at 19:20













                        98





                        +250









                        I think this topic is totally overdiscussed/overcomplicated. Using an IDE is THE way to go to completely avoid any syntax errors. I would even say that working without an IDE is kind of unprofessional. Why? Because modern IDEs check your syntax after every character you type. When you code and your entire line turns red, and a big warning notice shows you the exact type and the exact position of the syntax error, then there's absolutely no need to search for another solution.



                        Using a syntax-checking IDE means:



                        You'll (effectively) never run into syntax errors again, simply because you see them right as you type. Seriously.



                        Excellent IDEs with syntax check (all of them are available for Linux, Windows and Mac):





                        1. NetBeans [free]


                        2. PHPStorm [$199 USD]


                        3. Eclipse with PHP Plugin [free]


                        4. Sublime [$80 USD] (mainly a text editor, but expandable with plugins, like PHP Syntax Parser)






                        share|improve this answer





















                        • 1





                          It is obviously. However, relisting IDEs here, can you elaborate a bit where they differ in their syntax helpfulness? Sublime is mostly an editor, not IDE; but then more pretty and snappy; does primarily just syntax highlighing but's also veritable at bracket matching. It easily discovers T_CONSTANT_AND_ENCAPSED errors instantly for example, unlike PHPStorm; which however does more squiggly lines for inline errors. NetBeans´ syntax hints used to be more cryptic than PHPs even (relisting allowed constructs rather). Can you share your experience on pros/cons; is your favorite Eclipse/PDT or..?

                          – mario
                          Aug 12 '13 at 20:31











                        • @mario I think you are really deep into the topic so i really dont want to say anything wrong here, but all the code I (and my team mates, friends who code, freelance partners) have ever written in an IDE never ever was executed with a syntax error. So I think at least Netbeans/PHPStorm's syntax check is extremely powerful. But maybe I've misread your question. Gimme some hours ... ;)

                          – Sliq
                          Aug 12 '13 at 21:03











                        • Your answer is already spot on. Would fit 99% of our questions. However for the context here I'd like a trade-off consideration on which IDE provides the more newbie-friendly tooltips. It's probably minor to us, colorization and squiggly lines being sufficient if you're versed enough. But I presume the differences could be more significant to beginners.

                          – mario
                          Aug 12 '13 at 21:29











                        • Sometimes an IDE is not a feasible option. For example, making quick edits to a WordPress theme or plugin. Yes, I could copy all the code into an IDE, but then I have to open it up, paste it all in there, set headers and all that other time wasting crap, when I'm just hoping for a quick edit. Now, if you're developing new features or starting from scratch, then, yes, do it in an IDE. You won't regret taking that bit of extra time at the start to set it up.

                          – fredsbend
                          Mar 6 '17 at 16:22













                        • I see IDE as a trailer, not just a toolbox. It might not FIX but it can help you find and prevent syntax errors. Many answer here seems to say that if you keep you code clean you have less chance to make an error and are easier to spot. Well with auto-indentation, code hints, variable occurrence, auto-closing brackets and auto-formatting saves me many typos a day and is the main advantage why i use one. This is not counting everything else beyond the scope of this question (debugger, database connector, uml diagram etc.) IDE will save you time and will prevent more than just syntax errors.

                          – Louis Loudog Trottier
                          Mar 27 '17 at 4:48
















                        98





                        +250









                        I think this topic is totally overdiscussed/overcomplicated. Using an IDE is THE way to go to completely avoid any syntax errors. I would even say that working without an IDE is kind of unprofessional. Why? Because modern IDEs check your syntax after every character you type. When you code and your entire line turns red, and a big warning notice shows you the exact type and the exact position of the syntax error, then there's absolutely no need to search for another solution.



                        Using a syntax-checking IDE means:



                        You'll (effectively) never run into syntax errors again, simply because you see them right as you type. Seriously.



                        Excellent IDEs with syntax check (all of them are available for Linux, Windows and Mac):





                        1. NetBeans [free]


                        2. PHPStorm [$199 USD]


                        3. Eclipse with PHP Plugin [free]


                        4. Sublime [$80 USD] (mainly a text editor, but expandable with plugins, like PHP Syntax Parser)






                        share|improve this answer





















                        • 1





                          It is obviously. However, relisting IDEs here, can you elaborate a bit where they differ in their syntax helpfulness? Sublime is mostly an editor, not IDE; but then more pretty and snappy; does primarily just syntax highlighing but's also veritable at bracket matching. It easily discovers T_CONSTANT_AND_ENCAPSED errors instantly for example, unlike PHPStorm; which however does more squiggly lines for inline errors. NetBeans´ syntax hints used to be more cryptic than PHPs even (relisting allowed constructs rather). Can you share your experience on pros/cons; is your favorite Eclipse/PDT or..?

                          – mario
                          Aug 12 '13 at 20:31











                        • @mario I think you are really deep into the topic so i really dont want to say anything wrong here, but all the code I (and my team mates, friends who code, freelance partners) have ever written in an IDE never ever was executed with a syntax error. So I think at least Netbeans/PHPStorm's syntax check is extremely powerful. But maybe I've misread your question. Gimme some hours ... ;)

                          – Sliq
                          Aug 12 '13 at 21:03











                        • Your answer is already spot on. Would fit 99% of our questions. However for the context here I'd like a trade-off consideration on which IDE provides the more newbie-friendly tooltips. It's probably minor to us, colorization and squiggly lines being sufficient if you're versed enough. But I presume the differences could be more significant to beginners.

                          – mario
                          Aug 12 '13 at 21:29











                        • Sometimes an IDE is not a feasible option. For example, making quick edits to a WordPress theme or plugin. Yes, I could copy all the code into an IDE, but then I have to open it up, paste it all in there, set headers and all that other time wasting crap, when I'm just hoping for a quick edit. Now, if you're developing new features or starting from scratch, then, yes, do it in an IDE. You won't regret taking that bit of extra time at the start to set it up.

                          – fredsbend
                          Mar 6 '17 at 16:22













                        • I see IDE as a trailer, not just a toolbox. It might not FIX but it can help you find and prevent syntax errors. Many answer here seems to say that if you keep you code clean you have less chance to make an error and are easier to spot. Well with auto-indentation, code hints, variable occurrence, auto-closing brackets and auto-formatting saves me many typos a day and is the main advantage why i use one. This is not counting everything else beyond the scope of this question (debugger, database connector, uml diagram etc.) IDE will save you time and will prevent more than just syntax errors.

                          – Louis Loudog Trottier
                          Mar 27 '17 at 4:48














                        98





                        +250







                        98





                        +250



                        98




                        +250





                        I think this topic is totally overdiscussed/overcomplicated. Using an IDE is THE way to go to completely avoid any syntax errors. I would even say that working without an IDE is kind of unprofessional. Why? Because modern IDEs check your syntax after every character you type. When you code and your entire line turns red, and a big warning notice shows you the exact type and the exact position of the syntax error, then there's absolutely no need to search for another solution.



                        Using a syntax-checking IDE means:



                        You'll (effectively) never run into syntax errors again, simply because you see them right as you type. Seriously.



                        Excellent IDEs with syntax check (all of them are available for Linux, Windows and Mac):





                        1. NetBeans [free]


                        2. PHPStorm [$199 USD]


                        3. Eclipse with PHP Plugin [free]


                        4. Sublime [$80 USD] (mainly a text editor, but expandable with plugins, like PHP Syntax Parser)






                        share|improve this answer















                        I think this topic is totally overdiscussed/overcomplicated. Using an IDE is THE way to go to completely avoid any syntax errors. I would even say that working without an IDE is kind of unprofessional. Why? Because modern IDEs check your syntax after every character you type. When you code and your entire line turns red, and a big warning notice shows you the exact type and the exact position of the syntax error, then there's absolutely no need to search for another solution.



                        Using a syntax-checking IDE means:



                        You'll (effectively) never run into syntax errors again, simply because you see them right as you type. Seriously.



                        Excellent IDEs with syntax check (all of them are available for Linux, Windows and Mac):





                        1. NetBeans [free]


                        2. PHPStorm [$199 USD]


                        3. Eclipse with PHP Plugin [free]


                        4. Sublime [$80 USD] (mainly a text editor, but expandable with plugins, like PHP Syntax Parser)







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Feb 28 '18 at 0:05


























                        community wiki





                        5 revs, 4 users 72%
                        Sliq









                        • 1





                          It is obviously. However, relisting IDEs here, can you elaborate a bit where they differ in their syntax helpfulness? Sublime is mostly an editor, not IDE; but then more pretty and snappy; does primarily just syntax highlighing but's also veritable at bracket matching. It easily discovers T_CONSTANT_AND_ENCAPSED errors instantly for example, unlike PHPStorm; which however does more squiggly lines for inline errors. NetBeans´ syntax hints used to be more cryptic than PHPs even (relisting allowed constructs rather). Can you share your experience on pros/cons; is your favorite Eclipse/PDT or..?

                          – mario
                          Aug 12 '13 at 20:31











                        • @mario I think you are really deep into the topic so i really dont want to say anything wrong here, but all the code I (and my team mates, friends who code, freelance partners) have ever written in an IDE never ever was executed with a syntax error. So I think at least Netbeans/PHPStorm's syntax check is extremely powerful. But maybe I've misread your question. Gimme some hours ... ;)

                          – Sliq
                          Aug 12 '13 at 21:03











                        • Your answer is already spot on. Would fit 99% of our questions. However for the context here I'd like a trade-off consideration on which IDE provides the more newbie-friendly tooltips. It's probably minor to us, colorization and squiggly lines being sufficient if you're versed enough. But I presume the differences could be more significant to beginners.

                          – mario
                          Aug 12 '13 at 21:29











                        • Sometimes an IDE is not a feasible option. For example, making quick edits to a WordPress theme or plugin. Yes, I could copy all the code into an IDE, but then I have to open it up, paste it all in there, set headers and all that other time wasting crap, when I'm just hoping for a quick edit. Now, if you're developing new features or starting from scratch, then, yes, do it in an IDE. You won't regret taking that bit of extra time at the start to set it up.

                          – fredsbend
                          Mar 6 '17 at 16:22













                        • I see IDE as a trailer, not just a toolbox. It might not FIX but it can help you find and prevent syntax errors. Many answer here seems to say that if you keep you code clean you have less chance to make an error and are easier to spot. Well with auto-indentation, code hints, variable occurrence, auto-closing brackets and auto-formatting saves me many typos a day and is the main advantage why i use one. This is not counting everything else beyond the scope of this question (debugger, database connector, uml diagram etc.) IDE will save you time and will prevent more than just syntax errors.

                          – Louis Loudog Trottier
                          Mar 27 '17 at 4:48














                        • 1





                          It is obviously. However, relisting IDEs here, can you elaborate a bit where they differ in their syntax helpfulness? Sublime is mostly an editor, not IDE; but then more pretty and snappy; does primarily just syntax highlighing but's also veritable at bracket matching. It easily discovers T_CONSTANT_AND_ENCAPSED errors instantly for example, unlike PHPStorm; which however does more squiggly lines for inline errors. NetBeans´ syntax hints used to be more cryptic than PHPs even (relisting allowed constructs rather). Can you share your experience on pros/cons; is your favorite Eclipse/PDT or..?

                          – mario
                          Aug 12 '13 at 20:31











                        • @mario I think you are really deep into the topic so i really dont want to say anything wrong here, but all the code I (and my team mates, friends who code, freelance partners) have ever written in an IDE never ever was executed with a syntax error. So I think at least Netbeans/PHPStorm's syntax check is extremely powerful. But maybe I've misread your question. Gimme some hours ... ;)

                          – Sliq
                          Aug 12 '13 at 21:03











                        • Your answer is already spot on. Would fit 99% of our questions. However for the context here I'd like a trade-off consideration on which IDE provides the more newbie-friendly tooltips. It's probably minor to us, colorization and squiggly lines being sufficient if you're versed enough. But I presume the differences could be more significant to beginners.

                          – mario
                          Aug 12 '13 at 21:29











                        • Sometimes an IDE is not a feasible option. For example, making quick edits to a WordPress theme or plugin. Yes, I could copy all the code into an IDE, but then I have to open it up, paste it all in there, set headers and all that other time wasting crap, when I'm just hoping for a quick edit. Now, if you're developing new features or starting from scratch, then, yes, do it in an IDE. You won't regret taking that bit of extra time at the start to set it up.

                          – fredsbend
                          Mar 6 '17 at 16:22













                        • I see IDE as a trailer, not just a toolbox. It might not FIX but it can help you find and prevent syntax errors. Many answer here seems to say that if you keep you code clean you have less chance to make an error and are easier to spot. Well with auto-indentation, code hints, variable occurrence, auto-closing brackets and auto-formatting saves me many typos a day and is the main advantage why i use one. This is not counting everything else beyond the scope of this question (debugger, database connector, uml diagram etc.) IDE will save you time and will prevent more than just syntax errors.

                          – Louis Loudog Trottier
                          Mar 27 '17 at 4:48








                        1




                        1





                        It is obviously. However, relisting IDEs here, can you elaborate a bit where they differ in their syntax helpfulness? Sublime is mostly an editor, not IDE; but then more pretty and snappy; does primarily just syntax highlighing but's also veritable at bracket matching. It easily discovers T_CONSTANT_AND_ENCAPSED errors instantly for example, unlike PHPStorm; which however does more squiggly lines for inline errors. NetBeans´ syntax hints used to be more cryptic than PHPs even (relisting allowed constructs rather). Can you share your experience on pros/cons; is your favorite Eclipse/PDT or..?

                        – mario
                        Aug 12 '13 at 20:31





                        It is obviously. However, relisting IDEs here, can you elaborate a bit where they differ in their syntax helpfulness? Sublime is mostly an editor, not IDE; but then more pretty and snappy; does primarily just syntax highlighing but's also veritable at bracket matching. It easily discovers T_CONSTANT_AND_ENCAPSED errors instantly for example, unlike PHPStorm; which however does more squiggly lines for inline errors. NetBeans´ syntax hints used to be more cryptic than PHPs even (relisting allowed constructs rather). Can you share your experience on pros/cons; is your favorite Eclipse/PDT or..?

                        – mario
                        Aug 12 '13 at 20:31













                        @mario I think you are really deep into the topic so i really dont want to say anything wrong here, but all the code I (and my team mates, friends who code, freelance partners) have ever written in an IDE never ever was executed with a syntax error. So I think at least Netbeans/PHPStorm's syntax check is extremely powerful. But maybe I've misread your question. Gimme some hours ... ;)

                        – Sliq
                        Aug 12 '13 at 21:03





                        @mario I think you are really deep into the topic so i really dont want to say anything wrong here, but all the code I (and my team mates, friends who code, freelance partners) have ever written in an IDE never ever was executed with a syntax error. So I think at least Netbeans/PHPStorm's syntax check is extremely powerful. But maybe I've misread your question. Gimme some hours ... ;)

                        – Sliq
                        Aug 12 '13 at 21:03













                        Your answer is already spot on. Would fit 99% of our questions. However for the context here I'd like a trade-off consideration on which IDE provides the more newbie-friendly tooltips. It's probably minor to us, colorization and squiggly lines being sufficient if you're versed enough. But I presume the differences could be more significant to beginners.

                        – mario
                        Aug 12 '13 at 21:29





                        Your answer is already spot on. Would fit 99% of our questions. However for the context here I'd like a trade-off consideration on which IDE provides the more newbie-friendly tooltips. It's probably minor to us, colorization and squiggly lines being sufficient if you're versed enough. But I presume the differences could be more significant to beginners.

                        – mario
                        Aug 12 '13 at 21:29













                        Sometimes an IDE is not a feasible option. For example, making quick edits to a WordPress theme or plugin. Yes, I could copy all the code into an IDE, but then I have to open it up, paste it all in there, set headers and all that other time wasting crap, when I'm just hoping for a quick edit. Now, if you're developing new features or starting from scratch, then, yes, do it in an IDE. You won't regret taking that bit of extra time at the start to set it up.

                        – fredsbend
                        Mar 6 '17 at 16:22







                        Sometimes an IDE is not a feasible option. For example, making quick edits to a WordPress theme or plugin. Yes, I could copy all the code into an IDE, but then I have to open it up, paste it all in there, set headers and all that other time wasting crap, when I'm just hoping for a quick edit. Now, if you're developing new features or starting from scratch, then, yes, do it in an IDE. You won't regret taking that bit of extra time at the start to set it up.

                        – fredsbend
                        Mar 6 '17 at 16:22















                        I see IDE as a trailer, not just a toolbox. It might not FIX but it can help you find and prevent syntax errors. Many answer here seems to say that if you keep you code clean you have less chance to make an error and are easier to spot. Well with auto-indentation, code hints, variable occurrence, auto-closing brackets and auto-formatting saves me many typos a day and is the main advantage why i use one. This is not counting everything else beyond the scope of this question (debugger, database connector, uml diagram etc.) IDE will save you time and will prevent more than just syntax errors.

                        – Louis Loudog Trottier
                        Mar 27 '17 at 4:48





                        I see IDE as a trailer, not just a toolbox. It might not FIX but it can help you find and prevent syntax errors. Many answer here seems to say that if you keep you code clean you have less chance to make an error and are easier to spot. Well with auto-indentation, code hints, variable occurrence, auto-closing brackets and auto-formatting saves me many typos a day and is the main advantage why i use one. This is not counting everything else beyond the scope of this question (debugger, database connector, uml diagram etc.) IDE will save you time and will prevent more than just syntax errors.

                        – Louis Loudog Trottier
                        Mar 27 '17 at 4:48











                        48














                        Unexpected [



                        These days, the unexpected [ array bracket is commonly seen on outdated PHP versions. The short array syntax is available since PHP >= 5.4. Older installations only support array().



                        $php53 = array(1, 2, 3);
                        $php54 = [1, 2, 3];



                        Array function result dereferencing is likewise not available for older PHP versions:



                        $result = get_whatever()["key"];



                        Reference - What does this error mean in PHP? - "Syntax error, unexpected [" shows the most common and practical workarounds.



                        Though, you're always better off just upgrading your PHP installation. For shared webhosting plans, first research if e.g. SetHandler php56-fcgi can be used to enable a newer runtime.



                        See also:




                        • PHP syntax for dereferencing function result → possible as of PHP 5.4

                        • PHP syntax error, unexpected '['

                        • Shorthand for arrays: is there a literal syntax like {} or ?

                        • PHP 5.3.10 vs PHP 5.5.3 syntax error unexpected '['

                        • PHP Difference between array() and

                        • PHP Array Syntax Parse Error Left Square Bracket "["


                        BTW, there are also preprocessors and PHP 5.4 syntax down-converters if you're really clingy with older + slower PHP versions.



                        Other causes for Unexpected [ syntax errors



                        If it's not the PHP version mismatch, then it's oftentimes a plain typo or newcomer syntax mistake:





                        • You can't use array property declarations/expressions in classes, not even in PHP 7.



                          protected $var["x"] = "Nope";




                        • Confusing [ with opening curly braces { or parentheses ( is a common oversight.



                          foreach [$a as $b)



                          Or even:



                          function foobar[$a, $b, $c] {




                        • Or trying to dereference constants (before PHP 5.6) as arrays:



                          $var = const[123];



                          At least PHP interprets that const as a constant name.



                          If you meant to access an array variable (which is the typical cause here), then add the leading $ sigil - so it becomes a $varname.




                        • You are trying to use the global keyword on a member of an associative array. This is not valid syntax:



                          global $var['key'];






                        Unexpected ] closing square bracket



                        This is somewhat rarer, but there are also syntax accidents with the terminating array ] bracket.





                        • Again mismatches with ) parentheses or } curly braces are common:



                          function foobar($a, $b, $c] {




                        • Or trying to end an array where there isn't one:



                          $var = 2];


                          Which often occurs in multi-line and nested array declarations.



                          $array = [1,[2,3],4,[5,6[7,[8],[9,10]],11],12]],15];



                          If so, use your IDE for bracket matching to find any premature ] array closure. At the very least use more spacing and newlines to narrow it down.








                        share|improve this answer






























                          48














                          Unexpected [



                          These days, the unexpected [ array bracket is commonly seen on outdated PHP versions. The short array syntax is available since PHP >= 5.4. Older installations only support array().



                          $php53 = array(1, 2, 3);
                          $php54 = [1, 2, 3];



                          Array function result dereferencing is likewise not available for older PHP versions:



                          $result = get_whatever()["key"];



                          Reference - What does this error mean in PHP? - "Syntax error, unexpected [" shows the most common and practical workarounds.



                          Though, you're always better off just upgrading your PHP installation. For shared webhosting plans, first research if e.g. SetHandler php56-fcgi can be used to enable a newer runtime.



                          See also:




                          • PHP syntax for dereferencing function result → possible as of PHP 5.4

                          • PHP syntax error, unexpected '['

                          • Shorthand for arrays: is there a literal syntax like {} or ?

                          • PHP 5.3.10 vs PHP 5.5.3 syntax error unexpected '['

                          • PHP Difference between array() and

                          • PHP Array Syntax Parse Error Left Square Bracket "["


                          BTW, there are also preprocessors and PHP 5.4 syntax down-converters if you're really clingy with older + slower PHP versions.



                          Other causes for Unexpected [ syntax errors



                          If it's not the PHP version mismatch, then it's oftentimes a plain typo or newcomer syntax mistake:





                          • You can't use array property declarations/expressions in classes, not even in PHP 7.



                            protected $var["x"] = "Nope";




                          • Confusing [ with opening curly braces { or parentheses ( is a common oversight.



                            foreach [$a as $b)



                            Or even:



                            function foobar[$a, $b, $c] {




                          • Or trying to dereference constants (before PHP 5.6) as arrays:



                            $var = const[123];



                            At least PHP interprets that const as a constant name.



                            If you meant to access an array variable (which is the typical cause here), then add the leading $ sigil - so it becomes a $varname.




                          • You are trying to use the global keyword on a member of an associative array. This is not valid syntax:



                            global $var['key'];






                          Unexpected ] closing square bracket



                          This is somewhat rarer, but there are also syntax accidents with the terminating array ] bracket.





                          • Again mismatches with ) parentheses or } curly braces are common:



                            function foobar($a, $b, $c] {




                          • Or trying to end an array where there isn't one:



                            $var = 2];


                            Which often occurs in multi-line and nested array declarations.



                            $array = [1,[2,3],4,[5,6[7,[8],[9,10]],11],12]],15];



                            If so, use your IDE for bracket matching to find any premature ] array closure. At the very least use more spacing and newlines to narrow it down.








                          share|improve this answer




























                            48












                            48








                            48







                            Unexpected [



                            These days, the unexpected [ array bracket is commonly seen on outdated PHP versions. The short array syntax is available since PHP >= 5.4. Older installations only support array().



                            $php53 = array(1, 2, 3);
                            $php54 = [1, 2, 3];



                            Array function result dereferencing is likewise not available for older PHP versions:



                            $result = get_whatever()["key"];



                            Reference - What does this error mean in PHP? - "Syntax error, unexpected [" shows the most common and practical workarounds.



                            Though, you're always better off just upgrading your PHP installation. For shared webhosting plans, first research if e.g. SetHandler php56-fcgi can be used to enable a newer runtime.



                            See also:




                            • PHP syntax for dereferencing function result → possible as of PHP 5.4

                            • PHP syntax error, unexpected '['

                            • Shorthand for arrays: is there a literal syntax like {} or ?

                            • PHP 5.3.10 vs PHP 5.5.3 syntax error unexpected '['

                            • PHP Difference between array() and

                            • PHP Array Syntax Parse Error Left Square Bracket "["


                            BTW, there are also preprocessors and PHP 5.4 syntax down-converters if you're really clingy with older + slower PHP versions.



                            Other causes for Unexpected [ syntax errors



                            If it's not the PHP version mismatch, then it's oftentimes a plain typo or newcomer syntax mistake:





                            • You can't use array property declarations/expressions in classes, not even in PHP 7.



                              protected $var["x"] = "Nope";




                            • Confusing [ with opening curly braces { or parentheses ( is a common oversight.



                              foreach [$a as $b)



                              Or even:



                              function foobar[$a, $b, $c] {




                            • Or trying to dereference constants (before PHP 5.6) as arrays:



                              $var = const[123];



                              At least PHP interprets that const as a constant name.



                              If you meant to access an array variable (which is the typical cause here), then add the leading $ sigil - so it becomes a $varname.




                            • You are trying to use the global keyword on a member of an associative array. This is not valid syntax:



                              global $var['key'];






                            Unexpected ] closing square bracket



                            This is somewhat rarer, but there are also syntax accidents with the terminating array ] bracket.





                            • Again mismatches with ) parentheses or } curly braces are common:



                              function foobar($a, $b, $c] {




                            • Or trying to end an array where there isn't one:



                              $var = 2];


                              Which often occurs in multi-line and nested array declarations.



                              $array = [1,[2,3],4,[5,6[7,[8],[9,10]],11],12]],15];



                              If so, use your IDE for bracket matching to find any premature ] array closure. At the very least use more spacing and newlines to narrow it down.








                            share|improve this answer















                            Unexpected [



                            These days, the unexpected [ array bracket is commonly seen on outdated PHP versions. The short array syntax is available since PHP >= 5.4. Older installations only support array().



                            $php53 = array(1, 2, 3);
                            $php54 = [1, 2, 3];



                            Array function result dereferencing is likewise not available for older PHP versions:



                            $result = get_whatever()["key"];



                            Reference - What does this error mean in PHP? - "Syntax error, unexpected [" shows the most common and practical workarounds.



                            Though, you're always better off just upgrading your PHP installation. For shared webhosting plans, first research if e.g. SetHandler php56-fcgi can be used to enable a newer runtime.



                            See also:




                            • PHP syntax for dereferencing function result → possible as of PHP 5.4

                            • PHP syntax error, unexpected '['

                            • Shorthand for arrays: is there a literal syntax like {} or ?

                            • PHP 5.3.10 vs PHP 5.5.3 syntax error unexpected '['

                            • PHP Difference between array() and

                            • PHP Array Syntax Parse Error Left Square Bracket "["


                            BTW, there are also preprocessors and PHP 5.4 syntax down-converters if you're really clingy with older + slower PHP versions.



                            Other causes for Unexpected [ syntax errors



                            If it's not the PHP version mismatch, then it's oftentimes a plain typo or newcomer syntax mistake:





                            • You can't use array property declarations/expressions in classes, not even in PHP 7.



                              protected $var["x"] = "Nope";




                            • Confusing [ with opening curly braces { or parentheses ( is a common oversight.



                              foreach [$a as $b)



                              Or even:



                              function foobar[$a, $b, $c] {




                            • Or trying to dereference constants (before PHP 5.6) as arrays:



                              $var = const[123];



                              At least PHP interprets that const as a constant name.



                              If you meant to access an array variable (which is the typical cause here), then add the leading $ sigil - so it becomes a $varname.




                            • You are trying to use the global keyword on a member of an associative array. This is not valid syntax:



                              global $var['key'];






                            Unexpected ] closing square bracket



                            This is somewhat rarer, but there are also syntax accidents with the terminating array ] bracket.





                            • Again mismatches with ) parentheses or } curly braces are common:



                              function foobar($a, $b, $c] {




                            • Or trying to end an array where there isn't one:



                              $var = 2];


                              Which often occurs in multi-line and nested array declarations.



                              $array = [1,[2,3],4,[5,6[7,[8],[9,10]],11],12]],15];



                              If so, use your IDE for bracket matching to find any premature ] array closure. At the very least use more spacing and newlines to narrow it down.









                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 2 at 12:26


























                            community wiki





                            4 revs, 4 users 81%
                            mario
























                                42














                                Unexpected T_VARIABLE



                                An "unexpected T_VARIABLE" means that there's a literal $variable name, which doesn't fit into the current expression/statement structure.



                                purposefully abstract/inexact operator+$variable diagram





                                1. Missing semicolon



                                  It most commonly indicates a missing semicolon in the previous line. Variable assignments following a statement are a good indicator where to look:




                                  func1()
                                  $var = 1 + 2; # parse error in line +2



                                2. String concatenation



                                  A frequent mishap are string concatenations with forgotten . operator:




                                  print "Here comes the value: " $value;


                                  Btw, you should prefer string interpolation (basic variables in double quotes) whenever that helps readability. Which avoids these syntax issues.




                                  String interpolation is a scripting language core feature. No shame in utilizing it. Ignore any micro-optimization advise about variable . concatenation being faster. It's not.





                                3. Missing expression operators



                                  Of course the same issue can arise in other expressions, for instance arithmetic operations:




                                  print 4 + 7 $var;


                                  PHP can't guess here if the variable should have been added, subtracted or compared etc.




                                4. Lists



                                  Same for syntax lists, like in array populations, where the parser also indicates an expected comma , for example:




                                  $var = array("1" => $val, $val2, $val3 $val4);


                                  Or functions parameter lists:




                                  function myfunc($param1, $param2 $param3, $param4)


                                  Equivalently do you see this with list or global statements, or when lacking a ; semicolon in a for loop.




                                5. Class declarations



                                  This parser error also occurs in class declarations. You can only assign static constants, not expressions. Thus the parser complains about variables as assigned data:



                                  class xyz {      ⇓
                                  var $value = $_GET["input"];


                                  Unmatched } closing curly braces can in particular lead here. If a method is terminated too early (use proper indentation!), then a stray variable is commonly misplaced into the class declaration body.




                                6. Variables after identifiers



                                  You can also never have a variable follow an identifier directly:




                                  $this->myFunc$VAR();


                                  Btw, this is a common example where the intention was to use variable variables perhaps. In this case a variable property lookup with $this->{"myFunc$VAR"}(); for example.




                                  Take in mind that using variable variables should be the exception. Newcomers often try to use them too casually, even when arrays would be simpler and more appropriate.





                                7. Missing parens after language constructs



                                  Hasty typing may lead to forgotten opening parenthesis
                                  for if and for and foreach statements:




                                  foreach $array as $key) {


                                  Solution: add the missing opening ( between statement and variable.




                                8. Else does not expect conditions




                                  else ($var >= 0)


                                  Solution: Remove the conditions from else or use elseif.




                                9. Need brackets for closure




                                  function() uses $var {}


                                  Solution: Add brackets around $var.




                                10. Invisible whitespace



                                  As mentioned in the reference answer on "Invisible stray Unicode" (such as a non-breaking space), you might also see this error for unsuspecting code like:



                                  <?php

                                  $var = new PDO(...);


                                  It's rather prevalent in the start of files and for copy-and-pasted code. Check with a hexeditor, if your code does not visually appear to contain a syntax issue.




                                See also




                                • Search: unexpected T_VARIABLE






                                share|improve this answer






























                                  42














                                  Unexpected T_VARIABLE



                                  An "unexpected T_VARIABLE" means that there's a literal $variable name, which doesn't fit into the current expression/statement structure.



                                  purposefully abstract/inexact operator+$variable diagram





                                  1. Missing semicolon



                                    It most commonly indicates a missing semicolon in the previous line. Variable assignments following a statement are a good indicator where to look:




                                    func1()
                                    $var = 1 + 2; # parse error in line +2



                                  2. String concatenation



                                    A frequent mishap are string concatenations with forgotten . operator:




                                    print "Here comes the value: " $value;


                                    Btw, you should prefer string interpolation (basic variables in double quotes) whenever that helps readability. Which avoids these syntax issues.




                                    String interpolation is a scripting language core feature. No shame in utilizing it. Ignore any micro-optimization advise about variable . concatenation being faster. It's not.





                                  3. Missing expression operators



                                    Of course the same issue can arise in other expressions, for instance arithmetic operations:




                                    print 4 + 7 $var;


                                    PHP can't guess here if the variable should have been added, subtracted or compared etc.




                                  4. Lists



                                    Same for syntax lists, like in array populations, where the parser also indicates an expected comma , for example:




                                    $var = array("1" => $val, $val2, $val3 $val4);


                                    Or functions parameter lists:




                                    function myfunc($param1, $param2 $param3, $param4)


                                    Equivalently do you see this with list or global statements, or when lacking a ; semicolon in a for loop.




                                  5. Class declarations



                                    This parser error also occurs in class declarations. You can only assign static constants, not expressions. Thus the parser complains about variables as assigned data:



                                    class xyz {      ⇓
                                    var $value = $_GET["input"];


                                    Unmatched } closing curly braces can in particular lead here. If a method is terminated too early (use proper indentation!), then a stray variable is commonly misplaced into the class declaration body.




                                  6. Variables after identifiers



                                    You can also never have a variable follow an identifier directly:




                                    $this->myFunc$VAR();


                                    Btw, this is a common example where the intention was to use variable variables perhaps. In this case a variable property lookup with $this->{"myFunc$VAR"}(); for example.




                                    Take in mind that using variable variables should be the exception. Newcomers often try to use them too casually, even when arrays would be simpler and more appropriate.





                                  7. Missing parens after language constructs



                                    Hasty typing may lead to forgotten opening parenthesis
                                    for if and for and foreach statements:




                                    foreach $array as $key) {


                                    Solution: add the missing opening ( between statement and variable.




                                  8. Else does not expect conditions




                                    else ($var >= 0)


                                    Solution: Remove the conditions from else or use elseif.




                                  9. Need brackets for closure




                                    function() uses $var {}


                                    Solution: Add brackets around $var.




                                  10. Invisible whitespace



                                    As mentioned in the reference answer on "Invisible stray Unicode" (such as a non-breaking space), you might also see this error for unsuspecting code like:



                                    <?php

                                    $var = new PDO(...);


                                    It's rather prevalent in the start of files and for copy-and-pasted code. Check with a hexeditor, if your code does not visually appear to contain a syntax issue.




                                  See also




                                  • Search: unexpected T_VARIABLE






                                  share|improve this answer




























                                    42












                                    42








                                    42







                                    Unexpected T_VARIABLE



                                    An "unexpected T_VARIABLE" means that there's a literal $variable name, which doesn't fit into the current expression/statement structure.



                                    purposefully abstract/inexact operator+$variable diagram





                                    1. Missing semicolon



                                      It most commonly indicates a missing semicolon in the previous line. Variable assignments following a statement are a good indicator where to look:




                                      func1()
                                      $var = 1 + 2; # parse error in line +2



                                    2. String concatenation



                                      A frequent mishap are string concatenations with forgotten . operator:




                                      print "Here comes the value: " $value;


                                      Btw, you should prefer string interpolation (basic variables in double quotes) whenever that helps readability. Which avoids these syntax issues.




                                      String interpolation is a scripting language core feature. No shame in utilizing it. Ignore any micro-optimization advise about variable . concatenation being faster. It's not.





                                    3. Missing expression operators



                                      Of course the same issue can arise in other expressions, for instance arithmetic operations:




                                      print 4 + 7 $var;


                                      PHP can't guess here if the variable should have been added, subtracted or compared etc.




                                    4. Lists



                                      Same for syntax lists, like in array populations, where the parser also indicates an expected comma , for example:




                                      $var = array("1" => $val, $val2, $val3 $val4);


                                      Or functions parameter lists:




                                      function myfunc($param1, $param2 $param3, $param4)


                                      Equivalently do you see this with list or global statements, or when lacking a ; semicolon in a for loop.




                                    5. Class declarations



                                      This parser error also occurs in class declarations. You can only assign static constants, not expressions. Thus the parser complains about variables as assigned data:



                                      class xyz {      ⇓
                                      var $value = $_GET["input"];


                                      Unmatched } closing curly braces can in particular lead here. If a method is terminated too early (use proper indentation!), then a stray variable is commonly misplaced into the class declaration body.




                                    6. Variables after identifiers



                                      You can also never have a variable follow an identifier directly:




                                      $this->myFunc$VAR();


                                      Btw, this is a common example where the intention was to use variable variables perhaps. In this case a variable property lookup with $this->{"myFunc$VAR"}(); for example.




                                      Take in mind that using variable variables should be the exception. Newcomers often try to use them too casually, even when arrays would be simpler and more appropriate.





                                    7. Missing parens after language constructs



                                      Hasty typing may lead to forgotten opening parenthesis
                                      for if and for and foreach statements:




                                      foreach $array as $key) {


                                      Solution: add the missing opening ( between statement and variable.




                                    8. Else does not expect conditions




                                      else ($var >= 0)


                                      Solution: Remove the conditions from else or use elseif.




                                    9. Need brackets for closure




                                      function() uses $var {}


                                      Solution: Add brackets around $var.




                                    10. Invisible whitespace



                                      As mentioned in the reference answer on "Invisible stray Unicode" (such as a non-breaking space), you might also see this error for unsuspecting code like:



                                      <?php

                                      $var = new PDO(...);


                                      It's rather prevalent in the start of files and for copy-and-pasted code. Check with a hexeditor, if your code does not visually appear to contain a syntax issue.




                                    See also




                                    • Search: unexpected T_VARIABLE






                                    share|improve this answer















                                    Unexpected T_VARIABLE



                                    An "unexpected T_VARIABLE" means that there's a literal $variable name, which doesn't fit into the current expression/statement structure.



                                    purposefully abstract/inexact operator+$variable diagram





                                    1. Missing semicolon



                                      It most commonly indicates a missing semicolon in the previous line. Variable assignments following a statement are a good indicator where to look:




                                      func1()
                                      $var = 1 + 2; # parse error in line +2



                                    2. String concatenation



                                      A frequent mishap are string concatenations with forgotten . operator:




                                      print "Here comes the value: " $value;


                                      Btw, you should prefer string interpolation (basic variables in double quotes) whenever that helps readability. Which avoids these syntax issues.




                                      String interpolation is a scripting language core feature. No shame in utilizing it. Ignore any micro-optimization advise about variable . concatenation being faster. It's not.





                                    3. Missing expression operators



                                      Of course the same issue can arise in other expressions, for instance arithmetic operations:




                                      print 4 + 7 $var;


                                      PHP can't guess here if the variable should have been added, subtracted or compared etc.




                                    4. Lists



                                      Same for syntax lists, like in array populations, where the parser also indicates an expected comma , for example:




                                      $var = array("1" => $val, $val2, $val3 $val4);


                                      Or functions parameter lists:




                                      function myfunc($param1, $param2 $param3, $param4)


                                      Equivalently do you see this with list or global statements, or when lacking a ; semicolon in a for loop.




                                    5. Class declarations



                                      This parser error also occurs in class declarations. You can only assign static constants, not expressions. Thus the parser complains about variables as assigned data:



                                      class xyz {      ⇓
                                      var $value = $_GET["input"];


                                      Unmatched } closing curly braces can in particular lead here. If a method is terminated too early (use proper indentation!), then a stray variable is commonly misplaced into the class declaration body.




                                    6. Variables after identifiers



                                      You can also never have a variable follow an identifier directly:




                                      $this->myFunc$VAR();


                                      Btw, this is a common example where the intention was to use variable variables perhaps. In this case a variable property lookup with $this->{"myFunc$VAR"}(); for example.




                                      Take in mind that using variable variables should be the exception. Newcomers often try to use them too casually, even when arrays would be simpler and more appropriate.





                                    7. Missing parens after language constructs



                                      Hasty typing may lead to forgotten opening parenthesis
                                      for if and for and foreach statements:




                                      foreach $array as $key) {


                                      Solution: add the missing opening ( between statement and variable.




                                    8. Else does not expect conditions




                                      else ($var >= 0)


                                      Solution: Remove the conditions from else or use elseif.




                                    9. Need brackets for closure




                                      function() uses $var {}


                                      Solution: Add brackets around $var.




                                    10. Invisible whitespace



                                      As mentioned in the reference answer on "Invisible stray Unicode" (such as a non-breaking space), you might also see this error for unsuspecting code like:



                                      <?php

                                      $var = new PDO(...);


                                      It's rather prevalent in the start of files and for copy-and-pasted code. Check with a hexeditor, if your code does not visually appear to contain a syntax issue.




                                    See also




                                    • Search: unexpected T_VARIABLE







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Sep 18 '17 at 5:22


























                                    community wiki





                                    6 revs, 3 users 91%
                                    mario
























                                        31
















                                        Unexpected T_CONSTANT_ENCAPSED_STRING
                                        Unexpected T_ENCAPSED_AND_WHITESPACE



                                        The unwieldy names T_CONSTANT_ENCAPSED_STRING and T_ENCAPSED_AND_WHITESPACE refer to quoted "string" literals.



                                        They're used in different contexts, but the syntax issue are quite similar. T_ENCAPSED… warnings occur in double quoted string context, while T_CONSTANT… strings are often astray in plain PHP expressions or statements.





                                        1. Incorrect variable interpolation



                                          And it comes up most frequently for incorrect PHP variable interpolation:



                                                                    ⇓     ⇓
                                          echo "Here comes a $wrong['array'] access";


                                          Quoting arrays keys is a must in PHP context. But in double quoted strings (or HEREDOCs) this is a mistake. The parser complains about the contained single quoted 'string', because it usually expects a literal identifier / key there.



                                          More precisely it's valid to use PHP2-style simple syntax within double quotes for array references:



                                          echo "This is only $valid[here] ...";


                                          Nested arrays or deeper object references however require the complex curly string expression syntax:



                                          echo "Use {$array['as_usual']} with curly syntax.";


                                          If unsure, this is commonly safer to use. It's often even considered more readable. And better IDEs actually use distinct syntax colorization for that.




                                        2. Missing concatenation



                                          If a string follows an expression, but lacks a concatenation or other operator, then you'll see PHP complain about the string literal:




                                          print "Hello " . WORLD " !";


                                          While it's obvious to you and me, PHP just can't guess that the string was meant to be appended there.




                                        3. Confusing string quote enclosures



                                          The same syntax error occurs when confounding string delimiters. A string started by a single ' or double " quote also ends with the same.




                                          print "<a href="' . $link . '">click here</a>";
                                          ⌞⎽⎽⎽⎽⎽⎽⎽⎽⌟⌞⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⌟⌞⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⌟


                                          That example started with double quotes. But double quotes were also destined for the HTML attributes. The intended concatenation operator within however became interpreted as part of a second string in single quotes.




                                          Tip: Set your editor/IDE to use slightly distinct colorization for single and double quoted strings. (It also helps with application logic to prefer e.g. double quoted strings for textual output, and single quoted strings only for constant-like values.)




                                          This is a good example where you shouldn't break out of double quotes in the first place. Instead just use proper " escapes for the HTML attributes´ quotes:



                                          print "<a href="{$link}">click here</a>";


                                          While this can also lead to syntax confusion, all better IDEs/editors again help by colorizing the escaped quotes differently.




                                        4. Missing opening quote



                                          Equivalently are forgotten opening "/' quotes a recipe for parser errors:




                                          make_url(login', 'open');


                                          Here the ', ' would become a string literal after a bareword, when obviously login was meant to be a string parameter.




                                        5. Array lists



                                          If you miss a , comma in an array creation block, the parser will see two consecutive strings:



                                          array(               ⇓
                                          "key" => "value"
                                          "next" => "....",
                                          );


                                          Note that the last line may always contain an extra comma, but overlooking one in between is unforgivable. Which is hard to discover without syntax highlighting.




                                        6. Function parameter lists



                                          Same thing for function calls:




                                          myfunc(123, "text", "and" "more")



                                        7. Runaway strings



                                          A common variation are quite simply forgotten string terminators:




                                          mysql_evil("SELECT * FROM stuffs);
                                          print "'ok'";



                                          Here PHP complains about two string literals directly following each other. But the real cause is the unclosed previous string of course.




                                        See also




                                        • Interpolation (double quoted string) of Associative Arrays in PHP

                                        • PHP - syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

                                        • Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in PHP

                                        • Unexpected T_CONSTANT_ENCAPSED_STRING error in SQL Query






                                        share|improve this answer






























                                          31
















                                          Unexpected T_CONSTANT_ENCAPSED_STRING
                                          Unexpected T_ENCAPSED_AND_WHITESPACE



                                          The unwieldy names T_CONSTANT_ENCAPSED_STRING and T_ENCAPSED_AND_WHITESPACE refer to quoted "string" literals.



                                          They're used in different contexts, but the syntax issue are quite similar. T_ENCAPSED… warnings occur in double quoted string context, while T_CONSTANT… strings are often astray in plain PHP expressions or statements.





                                          1. Incorrect variable interpolation



                                            And it comes up most frequently for incorrect PHP variable interpolation:



                                                                      ⇓     ⇓
                                            echo "Here comes a $wrong['array'] access";


                                            Quoting arrays keys is a must in PHP context. But in double quoted strings (or HEREDOCs) this is a mistake. The parser complains about the contained single quoted 'string', because it usually expects a literal identifier / key there.



                                            More precisely it's valid to use PHP2-style simple syntax within double quotes for array references:



                                            echo "This is only $valid[here] ...";


                                            Nested arrays or deeper object references however require the complex curly string expression syntax:



                                            echo "Use {$array['as_usual']} with curly syntax.";


                                            If unsure, this is commonly safer to use. It's often even considered more readable. And better IDEs actually use distinct syntax colorization for that.




                                          2. Missing concatenation



                                            If a string follows an expression, but lacks a concatenation or other operator, then you'll see PHP complain about the string literal:




                                            print "Hello " . WORLD " !";


                                            While it's obvious to you and me, PHP just can't guess that the string was meant to be appended there.




                                          3. Confusing string quote enclosures



                                            The same syntax error occurs when confounding string delimiters. A string started by a single ' or double " quote also ends with the same.




                                            print "<a href="' . $link . '">click here</a>";
                                            ⌞⎽⎽⎽⎽⎽⎽⎽⎽⌟⌞⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⌟⌞⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⌟


                                            That example started with double quotes. But double quotes were also destined for the HTML attributes. The intended concatenation operator within however became interpreted as part of a second string in single quotes.




                                            Tip: Set your editor/IDE to use slightly distinct colorization for single and double quoted strings. (It also helps with application logic to prefer e.g. double quoted strings for textual output, and single quoted strings only for constant-like values.)




                                            This is a good example where you shouldn't break out of double quotes in the first place. Instead just use proper " escapes for the HTML attributes´ quotes:



                                            print "<a href="{$link}">click here</a>";


                                            While this can also lead to syntax confusion, all better IDEs/editors again help by colorizing the escaped quotes differently.




                                          4. Missing opening quote



                                            Equivalently are forgotten opening "/' quotes a recipe for parser errors:




                                            make_url(login', 'open');


                                            Here the ', ' would become a string literal after a bareword, when obviously login was meant to be a string parameter.




                                          5. Array lists



                                            If you miss a , comma in an array creation block, the parser will see two consecutive strings:



                                            array(               ⇓
                                            "key" => "value"
                                            "next" => "....",
                                            );


                                            Note that the last line may always contain an extra comma, but overlooking one in between is unforgivable. Which is hard to discover without syntax highlighting.




                                          6. Function parameter lists



                                            Same thing for function calls:




                                            myfunc(123, "text", "and" "more")



                                          7. Runaway strings



                                            A common variation are quite simply forgotten string terminators:




                                            mysql_evil("SELECT * FROM stuffs);
                                            print "'ok'";



                                            Here PHP complains about two string literals directly following each other. But the real cause is the unclosed previous string of course.




                                          See also




                                          • Interpolation (double quoted string) of Associative Arrays in PHP

                                          • PHP - syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

                                          • Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in PHP

                                          • Unexpected T_CONSTANT_ENCAPSED_STRING error in SQL Query






                                          share|improve this answer




























                                            31












                                            31








                                            31









                                            Unexpected T_CONSTANT_ENCAPSED_STRING
                                            Unexpected T_ENCAPSED_AND_WHITESPACE



                                            The unwieldy names T_CONSTANT_ENCAPSED_STRING and T_ENCAPSED_AND_WHITESPACE refer to quoted "string" literals.



                                            They're used in different contexts, but the syntax issue are quite similar. T_ENCAPSED… warnings occur in double quoted string context, while T_CONSTANT… strings are often astray in plain PHP expressions or statements.





                                            1. Incorrect variable interpolation



                                              And it comes up most frequently for incorrect PHP variable interpolation:



                                                                        ⇓     ⇓
                                              echo "Here comes a $wrong['array'] access";


                                              Quoting arrays keys is a must in PHP context. But in double quoted strings (or HEREDOCs) this is a mistake. The parser complains about the contained single quoted 'string', because it usually expects a literal identifier / key there.



                                              More precisely it's valid to use PHP2-style simple syntax within double quotes for array references:



                                              echo "This is only $valid[here] ...";


                                              Nested arrays or deeper object references however require the complex curly string expression syntax:



                                              echo "Use {$array['as_usual']} with curly syntax.";


                                              If unsure, this is commonly safer to use. It's often even considered more readable. And better IDEs actually use distinct syntax colorization for that.




                                            2. Missing concatenation



                                              If a string follows an expression, but lacks a concatenation or other operator, then you'll see PHP complain about the string literal:




                                              print "Hello " . WORLD " !";


                                              While it's obvious to you and me, PHP just can't guess that the string was meant to be appended there.




                                            3. Confusing string quote enclosures



                                              The same syntax error occurs when confounding string delimiters. A string started by a single ' or double " quote also ends with the same.




                                              print "<a href="' . $link . '">click here</a>";
                                              ⌞⎽⎽⎽⎽⎽⎽⎽⎽⌟⌞⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⌟⌞⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⌟


                                              That example started with double quotes. But double quotes were also destined for the HTML attributes. The intended concatenation operator within however became interpreted as part of a second string in single quotes.




                                              Tip: Set your editor/IDE to use slightly distinct colorization for single and double quoted strings. (It also helps with application logic to prefer e.g. double quoted strings for textual output, and single quoted strings only for constant-like values.)




                                              This is a good example where you shouldn't break out of double quotes in the first place. Instead just use proper " escapes for the HTML attributes´ quotes:



                                              print "<a href="{$link}">click here</a>";


                                              While this can also lead to syntax confusion, all better IDEs/editors again help by colorizing the escaped quotes differently.




                                            4. Missing opening quote



                                              Equivalently are forgotten opening "/' quotes a recipe for parser errors:




                                              make_url(login', 'open');


                                              Here the ', ' would become a string literal after a bareword, when obviously login was meant to be a string parameter.




                                            5. Array lists



                                              If you miss a , comma in an array creation block, the parser will see two consecutive strings:



                                              array(               ⇓
                                              "key" => "value"
                                              "next" => "....",
                                              );


                                              Note that the last line may always contain an extra comma, but overlooking one in between is unforgivable. Which is hard to discover without syntax highlighting.




                                            6. Function parameter lists



                                              Same thing for function calls:




                                              myfunc(123, "text", "and" "more")



                                            7. Runaway strings



                                              A common variation are quite simply forgotten string terminators:




                                              mysql_evil("SELECT * FROM stuffs);
                                              print "'ok'";



                                              Here PHP complains about two string literals directly following each other. But the real cause is the unclosed previous string of course.




                                            See also




                                            • Interpolation (double quoted string) of Associative Arrays in PHP

                                            • PHP - syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

                                            • Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in PHP

                                            • Unexpected T_CONSTANT_ENCAPSED_STRING error in SQL Query






                                            share|improve this answer

















                                            Unexpected T_CONSTANT_ENCAPSED_STRING
                                            Unexpected T_ENCAPSED_AND_WHITESPACE



                                            The unwieldy names T_CONSTANT_ENCAPSED_STRING and T_ENCAPSED_AND_WHITESPACE refer to quoted "string" literals.



                                            They're used in different contexts, but the syntax issue are quite similar. T_ENCAPSED… warnings occur in double quoted string context, while T_CONSTANT… strings are often astray in plain PHP expressions or statements.





                                            1. Incorrect variable interpolation



                                              And it comes up most frequently for incorrect PHP variable interpolation:



                                                                        ⇓     ⇓
                                              echo "Here comes a $wrong['array'] access";


                                              Quoting arrays keys is a must in PHP context. But in double quoted strings (or HEREDOCs) this is a mistake. The parser complains about the contained single quoted 'string', because it usually expects a literal identifier / key there.



                                              More precisely it's valid to use PHP2-style simple syntax within double quotes for array references:



                                              echo "This is only $valid[here] ...";


                                              Nested arrays or deeper object references however require the complex curly string expression syntax:



                                              echo "Use {$array['as_usual']} with curly syntax.";


                                              If unsure, this is commonly safer to use. It's often even considered more readable. And better IDEs actually use distinct syntax colorization for that.




                                            2. Missing concatenation



                                              If a string follows an expression, but lacks a concatenation or other operator, then you'll see PHP complain about the string literal:




                                              print "Hello " . WORLD " !";


                                              While it's obvious to you and me, PHP just can't guess that the string was meant to be appended there.




                                            3. Confusing string quote enclosures



                                              The same syntax error occurs when confounding string delimiters. A string started by a single ' or double " quote also ends with the same.




                                              print "<a href="' . $link . '">click here</a>";
                                              ⌞⎽⎽⎽⎽⎽⎽⎽⎽⌟⌞⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⌟⌞⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⌟


                                              That example started with double quotes. But double quotes were also destined for the HTML attributes. The intended concatenation operator within however became interpreted as part of a second string in single quotes.




                                              Tip: Set your editor/IDE to use slightly distinct colorization for single and double quoted strings. (It also helps with application logic to prefer e.g. double quoted strings for textual output, and single quoted strings only for constant-like values.)




                                              This is a good example where you shouldn't break out of double quotes in the first place. Instead just use proper " escapes for the HTML attributes´ quotes:



                                              print "<a href="{$link}">click here</a>";


                                              While this can also lead to syntax confusion, all better IDEs/editors again help by colorizing the escaped quotes differently.




                                            4. Missing opening quote



                                              Equivalently are forgotten opening "/' quotes a recipe for parser errors:




                                              make_url(login', 'open');


                                              Here the ', ' would become a string literal after a bareword, when obviously login was meant to be a string parameter.




                                            5. Array lists



                                              If you miss a , comma in an array creation block, the parser will see two consecutive strings:



                                              array(               ⇓
                                              "key" => "value"
                                              "next" => "....",
                                              );


                                              Note that the last line may always contain an extra comma, but overlooking one in between is unforgivable. Which is hard to discover without syntax highlighting.




                                            6. Function parameter lists



                                              Same thing for function calls:




                                              myfunc(123, "text", "and" "more")



                                            7. Runaway strings



                                              A common variation are quite simply forgotten string terminators:




                                              mysql_evil("SELECT * FROM stuffs);
                                              print "'ok'";



                                              Here PHP complains about two string literals directly following each other. But the real cause is the unclosed previous string of course.




                                            See also




                                            • Interpolation (double quoted string) of Associative Arrays in PHP

                                            • PHP - syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

                                            • Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in PHP

                                            • Unexpected T_CONSTANT_ENCAPSED_STRING error in SQL Query







                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Nov 26 '17 at 14:07


























                                            community wiki





                                            5 revs
                                            mario
























                                                22














                                                Unexpected T_STRING



                                                T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.





                                                1. Misquoted strings



                                                  This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:



                                                                 ⇓                  ⇓
                                                  echo "<a href="http://example.com">click here</a>";


                                                  Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping " double quotes, or ' single quotes - depending on which was used as string enclosure.




                                                  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.

                                                  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.

                                                  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.


                                                  See also What is the difference between single-quoted and double-quoted strings in PHP?.




                                                2. Unclosed strings



                                                  If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:




                                                  echo "Some text", $a_variable, "and some runaway string ;
                                                  success("finished");



                                                  It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.




                                                3. Non-programming string quotes



                                                  If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:



                                                  $text = ’Something something..’ + ”these ain't quotes”;


                                                  Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example ”these is interpreted as a constant identifier. But any following text literal is then seen as a bareword/T_STRING by the parser.




                                                4. The missing semicolon; again



                                                  If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:




                                                  func1()
                                                  function2();


                                                  PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.




                                                5. Short open tags and <?xml headers in PHP scripts



                                                  This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:




                                                  <?xml version="1.0"?>


                                                  PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.




                                                6. Invisible Unicode characters



                                                  A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:



                                                  <?php
                                                  print 123;


                                                  You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.



                                                  You can narrow it down with with adding redundant ; statement separators at each line start:



                                                  <?php
                                                  ;print 123;


                                                  The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.




                                                7. The `$` sign missing in front of variable names



                                                  Variables in PHP are represented by a dollar sign followed by the name of the variable.



                                                  The dollar sign ($) is a sigil that marks the identifier as a name of a variable. Without this sigil, the identifier could be a language keyword or a constant.



                                                  This is a common error when the PHP code was "translated" from code written in another language (C, Java, JavaScript, etc.). In such cases, a declaration of the variable type (when the original code was written in a language that uses typed variables) could also sneak out and produce this error.




                                                8. Escaped Quotation marks



                                                  If you use in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.



                                                  Example: echo 'Jim said 'Hello''; will print Jim said 'hello'



                                                  If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.



                                                  Very common error when specifiying paths in Windows: "C:xampphtdocs" is wrong. You need "C:\xampp\htdocs\".








                                                share|improve this answer






























                                                  22














                                                  Unexpected T_STRING



                                                  T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.





                                                  1. Misquoted strings



                                                    This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:



                                                                   ⇓                  ⇓
                                                    echo "<a href="http://example.com">click here</a>";


                                                    Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping " double quotes, or ' single quotes - depending on which was used as string enclosure.




                                                    • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.

                                                    • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.

                                                    • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.


                                                    See also What is the difference between single-quoted and double-quoted strings in PHP?.




                                                  2. Unclosed strings



                                                    If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:




                                                    echo "Some text", $a_variable, "and some runaway string ;
                                                    success("finished");



                                                    It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.




                                                  3. Non-programming string quotes



                                                    If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:



                                                    $text = ’Something something..’ + ”these ain't quotes”;


                                                    Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example ”these is interpreted as a constant identifier. But any following text literal is then seen as a bareword/T_STRING by the parser.




                                                  4. The missing semicolon; again



                                                    If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:




                                                    func1()
                                                    function2();


                                                    PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.




                                                  5. Short open tags and <?xml headers in PHP scripts



                                                    This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:




                                                    <?xml version="1.0"?>


                                                    PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.




                                                  6. Invisible Unicode characters



                                                    A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:



                                                    <?php
                                                    print 123;


                                                    You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.



                                                    You can narrow it down with with adding redundant ; statement separators at each line start:



                                                    <?php
                                                    ;print 123;


                                                    The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.




                                                  7. The `$` sign missing in front of variable names



                                                    Variables in PHP are represented by a dollar sign followed by the name of the variable.



                                                    The dollar sign ($) is a sigil that marks the identifier as a name of a variable. Without this sigil, the identifier could be a language keyword or a constant.



                                                    This is a common error when the PHP code was "translated" from code written in another language (C, Java, JavaScript, etc.). In such cases, a declaration of the variable type (when the original code was written in a language that uses typed variables) could also sneak out and produce this error.




                                                  8. Escaped Quotation marks



                                                    If you use in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.



                                                    Example: echo 'Jim said 'Hello''; will print Jim said 'hello'



                                                    If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.



                                                    Very common error when specifiying paths in Windows: "C:xampphtdocs" is wrong. You need "C:\xampp\htdocs\".








                                                  share|improve this answer




























                                                    22












                                                    22








                                                    22







                                                    Unexpected T_STRING



                                                    T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.





                                                    1. Misquoted strings



                                                      This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:



                                                                     ⇓                  ⇓
                                                      echo "<a href="http://example.com">click here</a>";


                                                      Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping " double quotes, or ' single quotes - depending on which was used as string enclosure.




                                                      • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.

                                                      • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.

                                                      • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.


                                                      See also What is the difference between single-quoted and double-quoted strings in PHP?.




                                                    2. Unclosed strings



                                                      If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:




                                                      echo "Some text", $a_variable, "and some runaway string ;
                                                      success("finished");



                                                      It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.




                                                    3. Non-programming string quotes



                                                      If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:



                                                      $text = ’Something something..’ + ”these ain't quotes”;


                                                      Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example ”these is interpreted as a constant identifier. But any following text literal is then seen as a bareword/T_STRING by the parser.




                                                    4. The missing semicolon; again



                                                      If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:




                                                      func1()
                                                      function2();


                                                      PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.




                                                    5. Short open tags and <?xml headers in PHP scripts



                                                      This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:




                                                      <?xml version="1.0"?>


                                                      PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.




                                                    6. Invisible Unicode characters



                                                      A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:



                                                      <?php
                                                      print 123;


                                                      You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.



                                                      You can narrow it down with with adding redundant ; statement separators at each line start:



                                                      <?php
                                                      ;print 123;


                                                      The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.




                                                    7. The `$` sign missing in front of variable names



                                                      Variables in PHP are represented by a dollar sign followed by the name of the variable.



                                                      The dollar sign ($) is a sigil that marks the identifier as a name of a variable. Without this sigil, the identifier could be a language keyword or a constant.



                                                      This is a common error when the PHP code was "translated" from code written in another language (C, Java, JavaScript, etc.). In such cases, a declaration of the variable type (when the original code was written in a language that uses typed variables) could also sneak out and produce this error.




                                                    8. Escaped Quotation marks



                                                      If you use in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.



                                                      Example: echo 'Jim said 'Hello''; will print Jim said 'hello'



                                                      If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.



                                                      Very common error when specifiying paths in Windows: "C:xampphtdocs" is wrong. You need "C:\xampp\htdocs\".








                                                    share|improve this answer















                                                    Unexpected T_STRING



                                                    T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.





                                                    1. Misquoted strings



                                                      This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:



                                                                     ⇓                  ⇓
                                                      echo "<a href="http://example.com">click here</a>";


                                                      Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping " double quotes, or ' single quotes - depending on which was used as string enclosure.




                                                      • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.

                                                      • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.

                                                      • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.


                                                      See also What is the difference between single-quoted and double-quoted strings in PHP?.




                                                    2. Unclosed strings



                                                      If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:




                                                      echo "Some text", $a_variable, "and some runaway string ;
                                                      success("finished");



                                                      It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.




                                                    3. Non-programming string quotes



                                                      If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:



                                                      $text = ’Something something..’ + ”these ain't quotes”;


                                                      Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example ”these is interpreted as a constant identifier. But any following text literal is then seen as a bareword/T_STRING by the parser.




                                                    4. The missing semicolon; again



                                                      If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:




                                                      func1()
                                                      function2();


                                                      PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.




                                                    5. Short open tags and <?xml headers in PHP scripts



                                                      This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:




                                                      <?xml version="1.0"?>


                                                      PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.




                                                    6. Invisible Unicode characters



                                                      A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:



                                                      <?php
                                                      print 123;


                                                      You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.



                                                      You can narrow it down with with adding redundant ; statement separators at each line start:



                                                      <?php
                                                      ;print 123;


                                                      The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.




                                                    7. The `$` sign missing in front of variable names



                                                      Variables in PHP are represented by a dollar sign followed by the name of the variable.



                                                      The dollar sign ($) is a sigil that marks the identifier as a name of a variable. Without this sigil, the identifier could be a language keyword or a constant.



                                                      This is a common error when the PHP code was "translated" from code written in another language (C, Java, JavaScript, etc.). In such cases, a declaration of the variable type (when the original code was written in a language that uses typed variables) could also sneak out and produce this error.




                                                    8. Escaped Quotation marks



                                                      If you use in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.



                                                      Example: echo 'Jim said 'Hello''; will print Jim said 'hello'



                                                      If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.



                                                      Very common error when specifiying paths in Windows: "C:xampphtdocs" is wrong. You need "C:\xampp\htdocs\".









                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Jul 19 '18 at 3:12


























                                                    community wiki





                                                    7 revs, 4 users 78%
                                                    mario
























                                                        15














                                                        Unexpected (



                                                        Opening parentheses typically follow language constructs such as if/foreach/for/array/list or start an arithmetic expression. They're syntactically incorrect after "strings", a previous (), a lone $, and in some typical declaration contexts.





                                                        1. Function declaration parameters



                                                          A rarer occurrence for this error is trying to use expressions as default function parameters. This is not supported, even in PHP7:



                                                          function header_fallback($value, $expires = time() + 90000) {


                                                          Parameters in a function declaration can only be literal values or constant expressions. Unlike for function invocations, where you can freely use whatever(1+something()*2) etc.




                                                        2. Class property defaults



                                                          Same thing for class member declarations, where only literal/constant values are allowed, not expressions:



                                                          class xyz {                   ⇓
                                                          var $default = get_config("xyz_default");


                                                          Put such things in the constructor.
                                                          See also Why don't PHP attributes allow functions?



                                                          Again note that PHP 7 only allows var $xy = 1 + 2 +3; constant expressions there.




                                                        3. JavaScript syntax in PHP



                                                          Using JavaScript or jQuery syntax won't work in PHP for obvious reasons:



                                                          <?php      ⇓
                                                          print $(document).text();


                                                          When this happens, it usually indicates an unterminated preceding string; and literal <script> sections leaking into PHP code context.




                                                        4. isset(()), empty, key, next, current



                                                          Both isset() and empty() are language built-ins, not functions. They need to access a variable directly. If you inadvertently add a pair of parentheses too much, then you'd create an expression however:




                                                          if (isset(($_GET["id"]))) {


                                                          The same applies to any language construct that requires implicit variable name access. These built-ins are part of the language grammar, therefore don't permit decorative extra parentheses.



                                                          User-level functions that require a variable reference -but get an expression result passed- lead to runtime errors instead.







                                                        Unexpected )





                                                        1. Absent function parameter



                                                          You cannot have stray commas last in a function call. PHP expects a value there and thusly complains about an early closing ) parenthesis.




                                                          callfunc(1, 2, );


                                                          A trailing comma is only allowed in array() or list() constructs.




                                                        2. Unfinished expressions



                                                          If you forget something in an arithmetic expression, then the parser gives up. Because how should it possibly interpret that:




                                                          $var = 2 * (1 + );


                                                          And if you forgot the closing ) even, then you'd get a complaint about the unexpected semicolon instead.




                                                        3. Foreach as constant



                                                          For forgotten variable $ prefixes in control statements you will see:



                                                                             ↓    ⇓
                                                          foreach ($array as wrong) {


                                                          PHP here sometimes tells you it expected a :: instead. Because a class::$variable could have satisfied the expected $variable expression..







                                                        Unexpected {



                                                        Curly braces { and } enclose code blocks. And syntax errors about them usually indicate some incorrec nesting.





                                                        1. Unmatched subexpressions in an if



                                                          Most commonly unbalanced ( and ) are the cause if the parser complains about the opening curly { appearing too early. A simple example:




                                                          if (($x == $y) && (2 == true) {


                                                          Count your parens or use an IDE which helps with that. Also don't write code without any spaces. Readability counts.




                                                        2. { and } in expression context



                                                          You can't use curly braces in expressions. If you confuse parentheses and curlys, it won't comply to the language grammer:




                                                          $var = 5 * {7 + $x};


                                                          There are a few exceptions for identifier construction, such as local scope variable ${references}.




                                                        3. Variable variables or curly var expressions



                                                          This is pretty rare. But you might also get { and } parser complaints for complex variable expressions:




                                                          print "Hello {$world[2{]} !";


                                                          Though there's a higher likelihood for an unexpected } in such contexts.







                                                        Unexpected }



                                                        When getting an "unexpected }" error, you've mostly closed a code block too early.





                                                        1. Last statement in a code block



                                                          It can happen for any unterminated expression.



                                                          And if the last line in a function/code block lacks a trailing ; semicolon:



                                                          function whatever() {
                                                          doStuff()
                                                          } ⇧


                                                          Here the parser can't tell if you perhaps still wanted to add + 25; to the function result or something else.




                                                        2. Invalid block nesting / Forgotten {



                                                          You'll sometimes see this parser error when a code block was } closed too early, or you forgot an opening { even:



                                                          function doStuff() {
                                                          if (true) ⇦
                                                          print "yes";
                                                          }
                                                          } ⇧


                                                          In above snippet the if didn't have an opening { curly brace. Thus the closing } one below became redundant. And therefore the next closing }, which was intended for the function, was not associatable to the original opening { curly brace.



                                                          Such errors are even harder to find without proper code indentation. Use an IDE and bracket matching.







                                                        Unexpected {, expecting (



                                                        Language constructs which require a condition/declaration header and a code block will trigger this error.





                                                        1. Parameter lists



                                                          For example misdeclared functions without parameter list are not permitted:




                                                          function whatever {
                                                          }



                                                        2. Control statement conditions



                                                          And you can't likewise have an if without condition.




                                                          if {
                                                          }


                                                          Which doesn't make sense, obviously. The same thing for the usual suspects, for/foreach, while/do, etc.




                                                          If you've got this particular error, you definitely should look up some manual examples.









                                                        share|improve this answer





















                                                        • 1





                                                          Was looking for answer to my question in this post, but found answer myself to the problem of - "Unexpected {", that`s why i wanted to share with my answer- for me the problem was line breaking encoding - somehow some of my files were using macintosh line breaks, but when i changed them to windows line breaks - my problem(on localhost(WAMP) everything works, but on linux webserver dont) was solved.

                                                          – Edgars Aivars
                                                          Aug 21 '17 at 20:36













                                                        • @EdgarsAivars Thanks for your comment! Platform-specific linebreaks are indeed an uncommon and tricky issue. I'll probably mention it within here as well. (It was just mentioned as aside in the other reference answer.)

                                                          – mario
                                                          Aug 21 '17 at 21:19











                                                        • I found that getting Unexpected } was because a piece of my code used the php short tag <? instead of <?php - took me a while to find this one as it worked on other servers.

                                                          – c7borg
                                                          Sep 28 '18 at 12:00
















                                                        15














                                                        Unexpected (



                                                        Opening parentheses typically follow language constructs such as if/foreach/for/array/list or start an arithmetic expression. They're syntactically incorrect after "strings", a previous (), a lone $, and in some typical declaration contexts.





                                                        1. Function declaration parameters



                                                          A rarer occurrence for this error is trying to use expressions as default function parameters. This is not supported, even in PHP7:



                                                          function header_fallback($value, $expires = time() + 90000) {


                                                          Parameters in a function declaration can only be literal values or constant expressions. Unlike for function invocations, where you can freely use whatever(1+something()*2) etc.




                                                        2. Class property defaults



                                                          Same thing for class member declarations, where only literal/constant values are allowed, not expressions:



                                                          class xyz {                   ⇓
                                                          var $default = get_config("xyz_default");


                                                          Put such things in the constructor.
                                                          See also Why don't PHP attributes allow functions?



                                                          Again note that PHP 7 only allows var $xy = 1 + 2 +3; constant expressions there.




                                                        3. JavaScript syntax in PHP



                                                          Using JavaScript or jQuery syntax won't work in PHP for obvious reasons:



                                                          <?php      ⇓
                                                          print $(document).text();


                                                          When this happens, it usually indicates an unterminated preceding string; and literal <script> sections leaking into PHP code context.




                                                        4. isset(()), empty, key, next, current



                                                          Both isset() and empty() are language built-ins, not functions. They need to access a variable directly. If you inadvertently add a pair of parentheses too much, then you'd create an expression however:




                                                          if (isset(($_GET["id"]))) {


                                                          The same applies to any language construct that requires implicit variable name access. These built-ins are part of the language grammar, therefore don't permit decorative extra parentheses.



                                                          User-level functions that require a variable reference -but get an expression result passed- lead to runtime errors instead.







                                                        Unexpected )





                                                        1. Absent function parameter



                                                          You cannot have stray commas last in a function call. PHP expects a value there and thusly complains about an early closing ) parenthesis.




                                                          callfunc(1, 2, );


                                                          A trailing comma is only allowed in array() or list() constructs.




                                                        2. Unfinished expressions



                                                          If you forget something in an arithmetic expression, then the parser gives up. Because how should it possibly interpret that:




                                                          $var = 2 * (1 + );


                                                          And if you forgot the closing ) even, then you'd get a complaint about the unexpected semicolon instead.




                                                        3. Foreach as constant



                                                          For forgotten variable $ prefixes in control statements you will see:



                                                                             ↓    ⇓
                                                          foreach ($array as wrong) {


                                                          PHP here sometimes tells you it expected a :: instead. Because a class::$variable could have satisfied the expected $variable expression..







                                                        Unexpected {



                                                        Curly braces { and } enclose code blocks. And syntax errors about them usually indicate some incorrec nesting.





                                                        1. Unmatched subexpressions in an if



                                                          Most commonly unbalanced ( and ) are the cause if the parser complains about the opening curly { appearing too early. A simple example:




                                                          if (($x == $y) && (2 == true) {


                                                          Count your parens or use an IDE which helps with that. Also don't write code without any spaces. Readability counts.




                                                        2. { and } in expression context



                                                          You can't use curly braces in expressions. If you confuse parentheses and curlys, it won't comply to the language grammer:




                                                          $var = 5 * {7 + $x};


                                                          There are a few exceptions for identifier construction, such as local scope variable ${references}.




                                                        3. Variable variables or curly var expressions



                                                          This is pretty rare. But you might also get { and } parser complaints for complex variable expressions:




                                                          print "Hello {$world[2{]} !";


                                                          Though there's a higher likelihood for an unexpected } in such contexts.







                                                        Unexpected }



                                                        When getting an "unexpected }" error, you've mostly closed a code block too early.





                                                        1. Last statement in a code block



                                                          It can happen for any unterminated expression.



                                                          And if the last line in a function/code block lacks a trailing ; semicolon:



                                                          function whatever() {
                                                          doStuff()
                                                          } ⇧


                                                          Here the parser can't tell if you perhaps still wanted to add + 25; to the function result or something else.




                                                        2. Invalid block nesting / Forgotten {



                                                          You'll sometimes see this parser error when a code block was } closed too early, or you forgot an opening { even:



                                                          function doStuff() {
                                                          if (true) ⇦
                                                          print "yes";
                                                          }
                                                          } ⇧


                                                          In above snippet the if didn't have an opening { curly brace. Thus the closing } one below became redundant. And therefore the next closing }, which was intended for the function, was not associatable to the original opening { curly brace.



                                                          Such errors are even harder to find without proper code indentation. Use an IDE and bracket matching.







                                                        Unexpected {, expecting (



                                                        Language constructs which require a condition/declaration header and a code block will trigger this error.





                                                        1. Parameter lists



                                                          For example misdeclared functions without parameter list are not permitted:




                                                          function whatever {
                                                          }



                                                        2. Control statement conditions



                                                          And you can't likewise have an if without condition.




                                                          if {
                                                          }


                                                          Which doesn't make sense, obviously. The same thing for the usual suspects, for/foreach, while/do, etc.




                                                          If you've got this particular error, you definitely should look up some manual examples.









                                                        share|improve this answer





















                                                        • 1





                                                          Was looking for answer to my question in this post, but found answer myself to the problem of - "Unexpected {", that`s why i wanted to share with my answer- for me the problem was line breaking encoding - somehow some of my files were using macintosh line breaks, but when i changed them to windows line breaks - my problem(on localhost(WAMP) everything works, but on linux webserver dont) was solved.

                                                          – Edgars Aivars
                                                          Aug 21 '17 at 20:36













                                                        • @EdgarsAivars Thanks for your comment! Platform-specific linebreaks are indeed an uncommon and tricky issue. I'll probably mention it within here as well. (It was just mentioned as aside in the other reference answer.)

                                                          – mario
                                                          Aug 21 '17 at 21:19











                                                        • I found that getting Unexpected } was because a piece of my code used the php short tag <? instead of <?php - took me a while to find this one as it worked on other servers.

                                                          – c7borg
                                                          Sep 28 '18 at 12:00














                                                        15












                                                        15








                                                        15







                                                        Unexpected (



                                                        Opening parentheses typically follow language constructs such as if/foreach/for/array/list or start an arithmetic expression. They're syntactically incorrect after "strings", a previous (), a lone $, and in some typical declaration contexts.





                                                        1. Function declaration parameters



                                                          A rarer occurrence for this error is trying to use expressions as default function parameters. This is not supported, even in PHP7:



                                                          function header_fallback($value, $expires = time() + 90000) {


                                                          Parameters in a function declaration can only be literal values or constant expressions. Unlike for function invocations, where you can freely use whatever(1+something()*2) etc.




                                                        2. Class property defaults



                                                          Same thing for class member declarations, where only literal/constant values are allowed, not expressions:



                                                          class xyz {                   ⇓
                                                          var $default = get_config("xyz_default");


                                                          Put such things in the constructor.
                                                          See also Why don't PHP attributes allow functions?



                                                          Again note that PHP 7 only allows var $xy = 1 + 2 +3; constant expressions there.




                                                        3. JavaScript syntax in PHP



                                                          Using JavaScript or jQuery syntax won't work in PHP for obvious reasons:



                                                          <?php      ⇓
                                                          print $(document).text();


                                                          When this happens, it usually indicates an unterminated preceding string; and literal <script> sections leaking into PHP code context.




                                                        4. isset(()), empty, key, next, current



                                                          Both isset() and empty() are language built-ins, not functions. They need to access a variable directly. If you inadvertently add a pair of parentheses too much, then you'd create an expression however:




                                                          if (isset(($_GET["id"]))) {


                                                          The same applies to any language construct that requires implicit variable name access. These built-ins are part of the language grammar, therefore don't permit decorative extra parentheses.



                                                          User-level functions that require a variable reference -but get an expression result passed- lead to runtime errors instead.







                                                        Unexpected )





                                                        1. Absent function parameter



                                                          You cannot have stray commas last in a function call. PHP expects a value there and thusly complains about an early closing ) parenthesis.




                                                          callfunc(1, 2, );


                                                          A trailing comma is only allowed in array() or list() constructs.




                                                        2. Unfinished expressions



                                                          If you forget something in an arithmetic expression, then the parser gives up. Because how should it possibly interpret that:




                                                          $var = 2 * (1 + );


                                                          And if you forgot the closing ) even, then you'd get a complaint about the unexpected semicolon instead.




                                                        3. Foreach as constant



                                                          For forgotten variable $ prefixes in control statements you will see:



                                                                             ↓    ⇓
                                                          foreach ($array as wrong) {


                                                          PHP here sometimes tells you it expected a :: instead. Because a class::$variable could have satisfied the expected $variable expression..







                                                        Unexpected {



                                                        Curly braces { and } enclose code blocks. And syntax errors about them usually indicate some incorrec nesting.





                                                        1. Unmatched subexpressions in an if



                                                          Most commonly unbalanced ( and ) are the cause if the parser complains about the opening curly { appearing too early. A simple example:




                                                          if (($x == $y) && (2 == true) {


                                                          Count your parens or use an IDE which helps with that. Also don't write code without any spaces. Readability counts.




                                                        2. { and } in expression context



                                                          You can't use curly braces in expressions. If you confuse parentheses and curlys, it won't comply to the language grammer:




                                                          $var = 5 * {7 + $x};


                                                          There are a few exceptions for identifier construction, such as local scope variable ${references}.




                                                        3. Variable variables or curly var expressions



                                                          This is pretty rare. But you might also get { and } parser complaints for complex variable expressions:




                                                          print "Hello {$world[2{]} !";


                                                          Though there's a higher likelihood for an unexpected } in such contexts.







                                                        Unexpected }



                                                        When getting an "unexpected }" error, you've mostly closed a code block too early.





                                                        1. Last statement in a code block



                                                          It can happen for any unterminated expression.



                                                          And if the last line in a function/code block lacks a trailing ; semicolon:



                                                          function whatever() {
                                                          doStuff()
                                                          } ⇧


                                                          Here the parser can't tell if you perhaps still wanted to add + 25; to the function result or something else.




                                                        2. Invalid block nesting / Forgotten {



                                                          You'll sometimes see this parser error when a code block was } closed too early, or you forgot an opening { even:



                                                          function doStuff() {
                                                          if (true) ⇦
                                                          print "yes";
                                                          }
                                                          } ⇧


                                                          In above snippet the if didn't have an opening { curly brace. Thus the closing } one below became redundant. And therefore the next closing }, which was intended for the function, was not associatable to the original opening { curly brace.



                                                          Such errors are even harder to find without proper code indentation. Use an IDE and bracket matching.







                                                        Unexpected {, expecting (



                                                        Language constructs which require a condition/declaration header and a code block will trigger this error.





                                                        1. Parameter lists



                                                          For example misdeclared functions without parameter list are not permitted:




                                                          function whatever {
                                                          }



                                                        2. Control statement conditions



                                                          And you can't likewise have an if without condition.




                                                          if {
                                                          }


                                                          Which doesn't make sense, obviously. The same thing for the usual suspects, for/foreach, while/do, etc.




                                                          If you've got this particular error, you definitely should look up some manual examples.









                                                        share|improve this answer















                                                        Unexpected (



                                                        Opening parentheses typically follow language constructs such as if/foreach/for/array/list or start an arithmetic expression. They're syntactically incorrect after "strings", a previous (), a lone $, and in some typical declaration contexts.





                                                        1. Function declaration parameters



                                                          A rarer occurrence for this error is trying to use expressions as default function parameters. This is not supported, even in PHP7:



                                                          function header_fallback($value, $expires = time() + 90000) {


                                                          Parameters in a function declaration can only be literal values or constant expressions. Unlike for function invocations, where you can freely use whatever(1+something()*2) etc.




                                                        2. Class property defaults



                                                          Same thing for class member declarations, where only literal/constant values are allowed, not expressions:



                                                          class xyz {                   ⇓
                                                          var $default = get_config("xyz_default");


                                                          Put such things in the constructor.
                                                          See also Why don't PHP attributes allow functions?



                                                          Again note that PHP 7 only allows var $xy = 1 + 2 +3; constant expressions there.




                                                        3. JavaScript syntax in PHP



                                                          Using JavaScript or jQuery syntax won't work in PHP for obvious reasons:



                                                          <?php      ⇓
                                                          print $(document).text();


                                                          When this happens, it usually indicates an unterminated preceding string; and literal <script> sections leaking into PHP code context.




                                                        4. isset(()), empty, key, next, current



                                                          Both isset() and empty() are language built-ins, not functions. They need to access a variable directly. If you inadvertently add a pair of parentheses too much, then you'd create an expression however:




                                                          if (isset(($_GET["id"]))) {


                                                          The same applies to any language construct that requires implicit variable name access. These built-ins are part of the language grammar, therefore don't permit decorative extra parentheses.



                                                          User-level functions that require a variable reference -but get an expression result passed- lead to runtime errors instead.







                                                        Unexpected )





                                                        1. Absent function parameter



                                                          You cannot have stray commas last in a function call. PHP expects a value there and thusly complains about an early closing ) parenthesis.




                                                          callfunc(1, 2, );


                                                          A trailing comma is only allowed in array() or list() constructs.




                                                        2. Unfinished expressions



                                                          If you forget something in an arithmetic expression, then the parser gives up. Because how should it possibly interpret that:




                                                          $var = 2 * (1 + );


                                                          And if you forgot the closing ) even, then you'd get a complaint about the unexpected semicolon instead.




                                                        3. Foreach as constant



                                                          For forgotten variable $ prefixes in control statements you will see:



                                                                             ↓    ⇓
                                                          foreach ($array as wrong) {


                                                          PHP here sometimes tells you it expected a :: instead. Because a class::$variable could have satisfied the expected $variable expression..







                                                        Unexpected {



                                                        Curly braces { and } enclose code blocks. And syntax errors about them usually indicate some incorrec nesting.





                                                        1. Unmatched subexpressions in an if



                                                          Most commonly unbalanced ( and ) are the cause if the parser complains about the opening curly { appearing too early. A simple example:




                                                          if (($x == $y) && (2 == true) {


                                                          Count your parens or use an IDE which helps with that. Also don't write code without any spaces. Readability counts.




                                                        2. { and } in expression context



                                                          You can't use curly braces in expressions. If you confuse parentheses and curlys, it won't comply to the language grammer:




                                                          $var = 5 * {7 + $x};


                                                          There are a few exceptions for identifier construction, such as local scope variable ${references}.




                                                        3. Variable variables or curly var expressions



                                                          This is pretty rare. But you might also get { and } parser complaints for complex variable expressions:




                                                          print "Hello {$world[2{]} !";


                                                          Though there's a higher likelihood for an unexpected } in such contexts.







                                                        Unexpected }



                                                        When getting an "unexpected }" error, you've mostly closed a code block too early.





                                                        1. Last statement in a code block



                                                          It can happen for any unterminated expression.



                                                          And if the last line in a function/code block lacks a trailing ; semicolon:



                                                          function whatever() {
                                                          doStuff()
                                                          } ⇧


                                                          Here the parser can't tell if you perhaps still wanted to add + 25; to the function result or something else.




                                                        2. Invalid block nesting / Forgotten {



                                                          You'll sometimes see this parser error when a code block was } closed too early, or you forgot an opening { even:



                                                          function doStuff() {
                                                          if (true) ⇦
                                                          print "yes";
                                                          }
                                                          } ⇧


                                                          In above snippet the if didn't have an opening { curly brace. Thus the closing } one below became redundant. And therefore the next closing }, which was intended for the function, was not associatable to the original opening { curly brace.



                                                          Such errors are even harder to find without proper code indentation. Use an IDE and bracket matching.







                                                        Unexpected {, expecting (



                                                        Language constructs which require a condition/declaration header and a code block will trigger this error.





                                                        1. Parameter lists



                                                          For example misdeclared functions without parameter list are not permitted:




                                                          function whatever {
                                                          }



                                                        2. Control statement conditions



                                                          And you can't likewise have an if without condition.




                                                          if {
                                                          }


                                                          Which doesn't make sense, obviously. The same thing for the usual suspects, for/foreach, while/do, etc.




                                                          If you've got this particular error, you definitely should look up some manual examples.










                                                        share|improve this answer














                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited Feb 28 '18 at 0:00


























                                                        community wiki





                                                        6 revs, 2 users 93%
                                                        mario









                                                        • 1





                                                          Was looking for answer to my question in this post, but found answer myself to the problem of - "Unexpected {", that`s why i wanted to share with my answer- for me the problem was line breaking encoding - somehow some of my files were using macintosh line breaks, but when i changed them to windows line breaks - my problem(on localhost(WAMP) everything works, but on linux webserver dont) was solved.

                                                          – Edgars Aivars
                                                          Aug 21 '17 at 20:36













                                                        • @EdgarsAivars Thanks for your comment! Platform-specific linebreaks are indeed an uncommon and tricky issue. I'll probably mention it within here as well. (It was just mentioned as aside in the other reference answer.)

                                                          – mario
                                                          Aug 21 '17 at 21:19











                                                        • I found that getting Unexpected } was because a piece of my code used the php short tag <? instead of <?php - took me a while to find this one as it worked on other servers.

                                                          – c7borg
                                                          Sep 28 '18 at 12:00














                                                        • 1





                                                          Was looking for answer to my question in this post, but found answer myself to the problem of - "Unexpected {", that`s why i wanted to share with my answer- for me the problem was line breaking encoding - somehow some of my files were using macintosh line breaks, but when i changed them to windows line breaks - my problem(on localhost(WAMP) everything works, but on linux webserver dont) was solved.

                                                          – Edgars Aivars
                                                          Aug 21 '17 at 20:36













                                                        • @EdgarsAivars Thanks for your comment! Platform-specific linebreaks are indeed an uncommon and tricky issue. I'll probably mention it within here as well. (It was just mentioned as aside in the other reference answer.)

                                                          – mario
                                                          Aug 21 '17 at 21:19











                                                        • I found that getting Unexpected } was because a piece of my code used the php short tag <? instead of <?php - took me a while to find this one as it worked on other servers.

                                                          – c7borg
                                                          Sep 28 '18 at 12:00








                                                        1




                                                        1





                                                        Was looking for answer to my question in this post, but found answer myself to the problem of - "Unexpected {", that`s why i wanted to share with my answer- for me the problem was line breaking encoding - somehow some of my files were using macintosh line breaks, but when i changed them to windows line breaks - my problem(on localhost(WAMP) everything works, but on linux webserver dont) was solved.

                                                        – Edgars Aivars
                                                        Aug 21 '17 at 20:36







                                                        Was looking for answer to my question in this post, but found answer myself to the problem of - "Unexpected {", that`s why i wanted to share with my answer- for me the problem was line breaking encoding - somehow some of my files were using macintosh line breaks, but when i changed them to windows line breaks - my problem(on localhost(WAMP) everything works, but on linux webserver dont) was solved.

                                                        – Edgars Aivars
                                                        Aug 21 '17 at 20:36















                                                        @EdgarsAivars Thanks for your comment! Platform-specific linebreaks are indeed an uncommon and tricky issue. I'll probably mention it within here as well. (It was just mentioned as aside in the other reference answer.)

                                                        – mario
                                                        Aug 21 '17 at 21:19





                                                        @EdgarsAivars Thanks for your comment! Platform-specific linebreaks are indeed an uncommon and tricky issue. I'll probably mention it within here as well. (It was just mentioned as aside in the other reference answer.)

                                                        – mario
                                                        Aug 21 '17 at 21:19













                                                        I found that getting Unexpected } was because a piece of my code used the php short tag <? instead of <?php - took me a while to find this one as it worked on other servers.

                                                        – c7borg
                                                        Sep 28 '18 at 12:00





                                                        I found that getting Unexpected } was because a piece of my code used the php short tag <? instead of <?php - took me a while to find this one as it worked on other servers.

                                                        – c7borg
                                                        Sep 28 '18 at 12:00











                                                        14














                                                        Unexpected $end



                                                        When PHP talks about an "unexpected $end", it means that your code ended prematurely. (The message is a bit misleading when taken literally. It's not about a variable named "$end", as sometimes assumed by newcomers. It refers to the "end of file", EOF.)




                                                        Cause: Unbalanced { and } for code blocks / and function or class declarations.




                                                        It's pretty much always about a missing } curly brace to close preceding code blocks.




                                                        • Again, use proper indentation to avoid such issues.



                                                        • Use an IDE with bracket matching, to find out where the } is amiss.
                                                          There are keyboard shortcuts in most IDEs and text editors:




                                                          • NetBeans, PhpStorm, Komodo: Ctrl[ and Ctrl]

                                                          • Eclipse, Aptana: CtrlShiftP

                                                          • Atom, Sublime: Ctrlm - Zend Studio CtrlM

                                                          • Geany, Notepad++: CtrlB - Joe: CtrlG - Emacs: C-M-n - Vim: %





                                                        Most IDEs also highlight matching braces, brackets and parentheses.
                                                        Which makes it pretty easy to inspect their correlation:



                                                        Bracket matching in an IDE



                                                        Unterminated expressions



                                                        And Unexpected $end syntax/parser error can also occur for unterminated expressions or statements:





                                                        • $var = func(1,
                                                          ?>EOF


                                                        So, look at the end of scripts first. A trailing ; is often redundant for the last statement in any PHP script. But you should have one. Precisely because it narrows such syntax issues down.



                                                        Indented HEREDOC markers



                                                        Another common occurrence appears with HEREDOC or NOWDOC strings. The terminating marker goes ignored with leading spaces, tabs, etc.:



                                                        print <<< END
                                                        Content...
                                                        Content....
                                                        END;
                                                        # ↑ terminator isn't exactly at the line start


                                                        Therefore the parser assumes the HEREDOC string to continue until the end of the file (hence "Unexpected $end"). Pretty much all IDEs and syntax-highlighting editors will make this obvious or warn about it.



                                                        Escaped Quotation marks



                                                        If you use in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.



                                                        Example: echo 'Jim said 'Hello''; will print Jim said 'hello'



                                                        If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.



                                                        Very common error when specifiying paths in Windows: "C:xampphtdocs" is wrong. You need "C:\xampp\htdocs\".



                                                        Alternative syntax



                                                        Somewhat rarer you can see this syntax error when using the alternative syntax for statement/code blocks in templates. Using if: and else: and a missing endif; for example.



                                                        See also:




                                                        • PHP syntax error “unexpected $end”

                                                        • Parse error: Syntax error, unexpected end of file in my PHP code

                                                        • Parse error syntax error unexpected end of file, using PHP

                                                        • PHP Parse error: syntax error, unexpected end of file in a CodeIgniter View

                                                        • Parse error: syntax error, unexpected end of file (Registration script)

                                                        • "Parse error: syntax error, unexpected $end" For my uni registration assignment

                                                        • Fixing PHP Errors: PHP Error #3: Unexpected end of file






                                                        share|improve this answer






























                                                          14














                                                          Unexpected $end



                                                          When PHP talks about an "unexpected $end", it means that your code ended prematurely. (The message is a bit misleading when taken literally. It's not about a variable named "$end", as sometimes assumed by newcomers. It refers to the "end of file", EOF.)




                                                          Cause: Unbalanced { and } for code blocks / and function or class declarations.




                                                          It's pretty much always about a missing } curly brace to close preceding code blocks.




                                                          • Again, use proper indentation to avoid such issues.



                                                          • Use an IDE with bracket matching, to find out where the } is amiss.
                                                            There are keyboard shortcuts in most IDEs and text editors:




                                                            • NetBeans, PhpStorm, Komodo: Ctrl[ and Ctrl]

                                                            • Eclipse, Aptana: CtrlShiftP

                                                            • Atom, Sublime: Ctrlm - Zend Studio CtrlM

                                                            • Geany, Notepad++: CtrlB - Joe: CtrlG - Emacs: C-M-n - Vim: %





                                                          Most IDEs also highlight matching braces, brackets and parentheses.
                                                          Which makes it pretty easy to inspect their correlation:



                                                          Bracket matching in an IDE



                                                          Unterminated expressions



                                                          And Unexpected $end syntax/parser error can also occur for unterminated expressions or statements:





                                                          • $var = func(1,
                                                            ?>EOF


                                                          So, look at the end of scripts first. A trailing ; is often redundant for the last statement in any PHP script. But you should have one. Precisely because it narrows such syntax issues down.



                                                          Indented HEREDOC markers



                                                          Another common occurrence appears with HEREDOC or NOWDOC strings. The terminating marker goes ignored with leading spaces, tabs, etc.:



                                                          print <<< END
                                                          Content...
                                                          Content....
                                                          END;
                                                          # ↑ terminator isn't exactly at the line start


                                                          Therefore the parser assumes the HEREDOC string to continue until the end of the file (hence "Unexpected $end"). Pretty much all IDEs and syntax-highlighting editors will make this obvious or warn about it.



                                                          Escaped Quotation marks



                                                          If you use in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.



                                                          Example: echo 'Jim said 'Hello''; will print Jim said 'hello'



                                                          If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.



                                                          Very common error when specifiying paths in Windows: "C:xampphtdocs" is wrong. You need "C:\xampp\htdocs\".



                                                          Alternative syntax



                                                          Somewhat rarer you can see this syntax error when using the alternative syntax for statement/code blocks in templates. Using if: and else: and a missing endif; for example.



                                                          See also:




                                                          • PHP syntax error “unexpected $end”

                                                          • Parse error: Syntax error, unexpected end of file in my PHP code

                                                          • Parse error syntax error unexpected end of file, using PHP

                                                          • PHP Parse error: syntax error, unexpected end of file in a CodeIgniter View

                                                          • Parse error: syntax error, unexpected end of file (Registration script)

                                                          • "Parse error: syntax error, unexpected $end" For my uni registration assignment

                                                          • Fixing PHP Errors: PHP Error #3: Unexpected end of file






                                                          share|improve this answer




























                                                            14












                                                            14








                                                            14







                                                            Unexpected $end



                                                            When PHP talks about an "unexpected $end", it means that your code ended prematurely. (The message is a bit misleading when taken literally. It's not about a variable named "$end", as sometimes assumed by newcomers. It refers to the "end of file", EOF.)




                                                            Cause: Unbalanced { and } for code blocks / and function or class declarations.




                                                            It's pretty much always about a missing } curly brace to close preceding code blocks.




                                                            • Again, use proper indentation to avoid such issues.



                                                            • Use an IDE with bracket matching, to find out where the } is amiss.
                                                              There are keyboard shortcuts in most IDEs and text editors:




                                                              • NetBeans, PhpStorm, Komodo: Ctrl[ and Ctrl]

                                                              • Eclipse, Aptana: CtrlShiftP

                                                              • Atom, Sublime: Ctrlm - Zend Studio CtrlM

                                                              • Geany, Notepad++: CtrlB - Joe: CtrlG - Emacs: C-M-n - Vim: %





                                                            Most IDEs also highlight matching braces, brackets and parentheses.
                                                            Which makes it pretty easy to inspect their correlation:



                                                            Bracket matching in an IDE



                                                            Unterminated expressions



                                                            And Unexpected $end syntax/parser error can also occur for unterminated expressions or statements:





                                                            • $var = func(1,
                                                              ?>EOF


                                                            So, look at the end of scripts first. A trailing ; is often redundant for the last statement in any PHP script. But you should have one. Precisely because it narrows such syntax issues down.



                                                            Indented HEREDOC markers



                                                            Another common occurrence appears with HEREDOC or NOWDOC strings. The terminating marker goes ignored with leading spaces, tabs, etc.:



                                                            print <<< END
                                                            Content...
                                                            Content....
                                                            END;
                                                            # ↑ terminator isn't exactly at the line start


                                                            Therefore the parser assumes the HEREDOC string to continue until the end of the file (hence "Unexpected $end"). Pretty much all IDEs and syntax-highlighting editors will make this obvious or warn about it.



                                                            Escaped Quotation marks



                                                            If you use in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.



                                                            Example: echo 'Jim said 'Hello''; will print Jim said 'hello'



                                                            If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.



                                                            Very common error when specifiying paths in Windows: "C:xampphtdocs" is wrong. You need "C:\xampp\htdocs\".



                                                            Alternative syntax



                                                            Somewhat rarer you can see this syntax error when using the alternative syntax for statement/code blocks in templates. Using if: and else: and a missing endif; for example.



                                                            See also:




                                                            • PHP syntax error “unexpected $end”

                                                            • Parse error: Syntax error, unexpected end of file in my PHP code

                                                            • Parse error syntax error unexpected end of file, using PHP

                                                            • PHP Parse error: syntax error, unexpected end of file in a CodeIgniter View

                                                            • Parse error: syntax error, unexpected end of file (Registration script)

                                                            • "Parse error: syntax error, unexpected $end" For my uni registration assignment

                                                            • Fixing PHP Errors: PHP Error #3: Unexpected end of file






                                                            share|improve this answer















                                                            Unexpected $end



                                                            When PHP talks about an "unexpected $end", it means that your code ended prematurely. (The message is a bit misleading when taken literally. It's not about a variable named "$end", as sometimes assumed by newcomers. It refers to the "end of file", EOF.)




                                                            Cause: Unbalanced { and } for code blocks / and function or class declarations.




                                                            It's pretty much always about a missing } curly brace to close preceding code blocks.




                                                            • Again, use proper indentation to avoid such issues.



                                                            • Use an IDE with bracket matching, to find out where the } is amiss.
                                                              There are keyboard shortcuts in most IDEs and text editors:




                                                              • NetBeans, PhpStorm, Komodo: Ctrl[ and Ctrl]

                                                              • Eclipse, Aptana: CtrlShiftP

                                                              • Atom, Sublime: Ctrlm - Zend Studio CtrlM

                                                              • Geany, Notepad++: CtrlB - Joe: CtrlG - Emacs: C-M-n - Vim: %





                                                            Most IDEs also highlight matching braces, brackets and parentheses.
                                                            Which makes it pretty easy to inspect their correlation:



                                                            Bracket matching in an IDE



                                                            Unterminated expressions



                                                            And Unexpected $end syntax/parser error can also occur for unterminated expressions or statements:





                                                            • $var = func(1,
                                                              ?>EOF


                                                            So, look at the end of scripts first. A trailing ; is often redundant for the last statement in any PHP script. But you should have one. Precisely because it narrows such syntax issues down.



                                                            Indented HEREDOC markers



                                                            Another common occurrence appears with HEREDOC or NOWDOC strings. The terminating marker goes ignored with leading spaces, tabs, etc.:



                                                            print <<< END
                                                            Content...
                                                            Content....
                                                            END;
                                                            # ↑ terminator isn't exactly at the line start


                                                            Therefore the parser assumes the HEREDOC string to continue until the end of the file (hence "Unexpected $end"). Pretty much all IDEs and syntax-highlighting editors will make this obvious or warn about it.



                                                            Escaped Quotation marks



                                                            If you use in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.



                                                            Example: echo 'Jim said 'Hello''; will print Jim said 'hello'



                                                            If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.



                                                            Very common error when specifiying paths in Windows: "C:xampphtdocs" is wrong. You need "C:\xampp\htdocs\".



                                                            Alternative syntax



                                                            Somewhat rarer you can see this syntax error when using the alternative syntax for statement/code blocks in templates. Using if: and else: and a missing endif; for example.



                                                            See also:




                                                            • PHP syntax error “unexpected $end”

                                                            • Parse error: Syntax error, unexpected end of file in my PHP code

                                                            • Parse error syntax error unexpected end of file, using PHP

                                                            • PHP Parse error: syntax error, unexpected end of file in a CodeIgniter View

                                                            • Parse error: syntax error, unexpected end of file (Registration script)

                                                            • "Parse error: syntax error, unexpected $end" For my uni registration assignment

                                                            • Fixing PHP Errors: PHP Error #3: Unexpected end of file







                                                            share|improve this answer














                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited Jul 19 '18 at 3:18


























                                                            community wiki





                                                            7 revs, 3 users 71%
                                                            mario
























                                                                13















                                                                Unexpected T_IF

                                                                Unexpected T_ELSEIF

                                                                Unexpected T_ELSE

                                                                Unexpected T_ENDIF



                                                                Conditional control blocks if, elseif and else follow a simple structure. When you encounter a syntax error, it's most likely just invalid block nesting → with missing { curly braces } - or one too many.



                                                                enter image description here





                                                                1. Missing { or } due to incorrect indentation



                                                                  Mismatched code braces are common to less well-formatted code such as:



                                                                  if((!($opt["uniQartz5.8"]!=$this->check58)) or (empty($_POST['poree']))) {if
                                                                  ($true) {echo"halp";} elseif((!$z)or%b){excSmthng(False,5.8)}elseif (False){


                                                                  If your code looks like this, start afresh! Otherwise it's unfixable to you or anyone else. There's no point in showcasing this on the internet to inquire for help.



                                                                  You will only be able to fix it, if you can visually follow the nested structure and relation of if/else conditionals and their { code blocks }. Use your IDE to see if they're all paired.



                                                                  if (true) {
                                                                  if (false) {

                                                                  }
                                                                  elseif ($whatever) {
                                                                  if ($something2) {

                                                                  }
                                                                  else {

                                                                  }
                                                                  }
                                                                  else {

                                                                  }
                                                                  if (false) { // a second `if` tree

                                                                  }
                                                                  else {

                                                                  }
                                                                  }
                                                                  elseif (false) {

                                                                  }


                                                                  Any double } } will not just close a branch, but a previous condition structure. Therefore stick with one coding style; don't mix and match in nested if/else trees.



                                                                  Apart from consistency here, it turns out helpful to avoid lengthy conditions too. Use temporary variables or functions to avoid unreadable if-expressions.




                                                                2. IF cannot be used in expressions



                                                                  A surprisingly frequent newcomer mistake is trying to use an if statement in an expression, such as a print statement:




                                                                  echo "<a href='" . if ($link == "example.org") { echo …


                                                                  Which is invalid of course.



                                                                  You can use a ternary conditional, but beware of readability impacts.



                                                                  echo "<a href='" . ($link ? "http://yes" : "http://no") . "</a>";


                                                                  Otherwise break such output constructs up: use multiple ifs and echos.

                                                                  Better yet, use temporary variables, and place your conditionals before:



                                                                  if ($link) { $href = "yes"; } else { $href = "no"; }
                                                                  echo "<a href='$href'>Link</a>";


                                                                  Defining functions or methods for such cases often makes sense too.



                                                                  Control blocks don't return "results"



                                                                  Now this is less common, but a few coders even try to treat if as if it could return a result:



                                                                  $var = if ($x == $y) { "true" };


                                                                  Which is structurally identical to using if within a string concatenation / expression.




                                                                  • But control structures (if / foreach / while) don't have a "result".

                                                                  • The literal string "true" would also just be a void statement.





                                                                  You'll have to use an assignment in the code block:



                                                                  if ($x == $y) { $var = "true"; }


                                                                  Alternatively, resort to a ?: ternary comparison.



                                                                  If in If



                                                                  You cannot nest an if within a condition either:




                                                                  if ($x == true and (if $y != false)) { ... }


                                                                  Which is obviously redundant, because the and (or or) already allows chaining comparisons.




                                                                3. Forgotton ; semicolons



                                                                  Once more: Each control block needs to be a statement. If the previous code piece isn't terminated by a semicolon, then that's a guaranteed syntax error:




                                                                  $var = 1 + 2 + 3
                                                                  if (true) { … }


                                                                  Btw, the last line in a {…} code block needs a semicolon too.




                                                                4. Semicolon too early



                                                                  Now it's probably wrong to blame a particular coding style, as this pitfall is too easy to overlook:




                                                                  if ($x == 5);
                                                                  {
                                                                  $y = 7;
                                                                  }
                                                                  else ←
                                                                  {
                                                                  $x = -1;
                                                                  }


                                                                  Which happens more often than you might imagine.




                                                                  • When you terminate the if () expression with ; it will execute a void statement. The ; becomes a an empty {} of its own!

                                                                  • The {…} block thus is detached from the if, and would always run.

                                                                  • So the else no longer had a relation to an open if construct,
                                                                    which is why this would lead to an Unexpected T_ELSE syntax error.





                                                                  Which also explains a likewise subtle variation of this syntax error:



                                                                  if ($x) { x_is_true(); }; else { something_else(); };


                                                                  Where the ; after the code block {…} terminates the whole if
                                                                  construct, severing the else branch syntactically.




                                                                5. Not using code blocks



                                                                  It's syntactically allowed to omit curly braces {} for code blocks in if/elseif/else branches. Which sadly is a syntax style very common to unversed coders. (Under the false assumption this was quicker to type or read).



                                                                  However that's highly likely to trip up the syntax. Sooner or later additional statements will find their way into the if/else branches:



                                                                  if (true)
                                                                  $x = 5;
                                                                  elseif (false)
                                                                  $x = 6;
                                                                  $y = 7; ←
                                                                  else
                                                                  $z = 0;


                                                                  But to actually use code blocks, you do have to write {} them as such!




                                                                  Even seasoned programmers avoid this braceless syntax, or at least
                                                                  understand it as an exceptional exception to the rule.





                                                                6. Else / Elseif in wrong order



                                                                  One thing to remind yourself is the conditional order, of course.



                                                                  if ($a) { … }
                                                                  else { … }
                                                                  elseif ($b) { … }



                                                                  You can have as many elseifs as you want, but else has to go last. That's just how it is.




                                                                7. Class declarations



                                                                  As mentioned above, you can't have control statements in a class declaration:



                                                                  class xyz {
                                                                  if (true) {
                                                                  function ($var) {}
                                                                  }


                                                                  You either forgot a function definition, or closed one } too early in such cases.




                                                                8. Unexpected T_ELSEIF / T_ELSE



                                                                  When mixing PHP and HTML, the closing } for an if/elseif must be in the same PHP block <?php ?> as the next elseif/else. This will generate an error as the closing } for the if needs to be part of the elseif:



                                                                  <?php if ($x) { ?>
                                                                  html
                                                                  <?php } ?>
                                                                  <?php elseif ($y) { ?>
                                                                  html
                                                                  <?php } ?>


                                                                  The correct form <?php } elseif:



                                                                  <?php if ($x) { ?>
                                                                  html
                                                                  <?php } elseif ($y) { ?>
                                                                  html
                                                                  <?php } ?>


                                                                  This is more or less a variation of incorrect indentation - presumably often based on wrong coding intentions.

                                                                  You cannot mash other statements inbetween if and elseif/else structural tokens:



                                                                  if (true) {
                                                                  }
                                                                  echo "in between"; ←
                                                                  elseif (false) {
                                                                  }
                                                                  ?> text <?php ←
                                                                  else {
                                                                  }


                                                                  Either can only occur in {…} code blocks, not in between control structure tokens.




                                                                  • This wouldn't make sense anyway. It's not like that there was some "undefined" state when PHP jumps between if and else branches.

                                                                  • You'll have to make up your mind where print statements belong to / or if they need to be repeated in both branches.





                                                                  Nor can you part an if/else between different control structures:



                                                                  foreach ($array as $i) {
                                                                  if ($i) { … }
                                                                  }
                                                                  else { … }


                                                                  There is no syntactic relation between the if and else. The foreach lexical scope ends at }, so there's no point for the if structure to continue.




                                                                9. T_ENDIF



                                                                  If an unexpected T_ENDIF is complained about, you're using the alternative syntax style if:elseif:else:endif;. Which you should really think twice about.




                                                                  • A common pitfall is confusing the eerily similar : colon for a ; semicolon. (Covered in "Semicolon too early")


                                                                  • As indentation is harder to track in template files, the more when using the alternative syntax - it's plausible your endif; does not match any if:.


                                                                  • Using } endif;
                                                                    is a doubled if-terminator.




                                                                  While an "unexpected $end" is usually the price for a forgotten closing } curly brace.




                                                                10. Assignment vs. comparison



                                                                  So, this is not a syntax error, but worth mentioning in this context:




                                                                  if ($x = true) { }
                                                                  else { do_false(); }


                                                                  That's not a ==/=== comparison, but an = assignment. This is rather subtle, and will easily lead some users to helplessly edit whole condition blocks. Watch out for unintended assignments first - whenver you experience a logic fault / misbeheviour.








                                                                share|improve this answer






























                                                                  13















                                                                  Unexpected T_IF

                                                                  Unexpected T_ELSEIF

                                                                  Unexpected T_ELSE

                                                                  Unexpected T_ENDIF



                                                                  Conditional control blocks if, elseif and else follow a simple structure. When you encounter a syntax error, it's most likely just invalid block nesting → with missing { curly braces } - or one too many.



                                                                  enter image description here





                                                                  1. Missing { or } due to incorrect indentation



                                                                    Mismatched code braces are common to less well-formatted code such as:



                                                                    if((!($opt["uniQartz5.8"]!=$this->check58)) or (empty($_POST['poree']))) {if
                                                                    ($true) {echo"halp";} elseif((!$z)or%b){excSmthng(False,5.8)}elseif (False){


                                                                    If your code looks like this, start afresh! Otherwise it's unfixable to you or anyone else. There's no point in showcasing this on the internet to inquire for help.



                                                                    You will only be able to fix it, if you can visually follow the nested structure and relation of if/else conditionals and their { code blocks }. Use your IDE to see if they're all paired.



                                                                    if (true) {
                                                                    if (false) {

                                                                    }
                                                                    elseif ($whatever) {
                                                                    if ($something2) {

                                                                    }
                                                                    else {

                                                                    }
                                                                    }
                                                                    else {

                                                                    }
                                                                    if (false) { // a second `if` tree

                                                                    }
                                                                    else {

                                                                    }
                                                                    }
                                                                    elseif (false) {

                                                                    }


                                                                    Any double } } will not just close a branch, but a previous condition structure. Therefore stick with one coding style; don't mix and match in nested if/else trees.



                                                                    Apart from consistency here, it turns out helpful to avoid lengthy conditions too. Use temporary variables or functions to avoid unreadable if-expressions.




                                                                  2. IF cannot be used in expressions



                                                                    A surprisingly frequent newcomer mistake is trying to use an if statement in an expression, such as a print statement:




                                                                    echo "<a href='" . if ($link == "example.org") { echo …


                                                                    Which is invalid of course.



                                                                    You can use a ternary conditional, but beware of readability impacts.



                                                                    echo "<a href='" . ($link ? "http://yes" : "http://no") . "</a>";


                                                                    Otherwise break such output constructs up: use multiple ifs and echos.

                                                                    Better yet, use temporary variables, and place your conditionals before:



                                                                    if ($link) { $href = "yes"; } else { $href = "no"; }
                                                                    echo "<a href='$href'>Link</a>";


                                                                    Defining functions or methods for such cases often makes sense too.



                                                                    Control blocks don't return "results"



                                                                    Now this is less common, but a few coders even try to treat if as if it could return a result:



                                                                    $var = if ($x == $y) { "true" };


                                                                    Which is structurally identical to using if within a string concatenation / expression.




                                                                    • But control structures (if / foreach / while) don't have a "result".

                                                                    • The literal string "true" would also just be a void statement.





                                                                    You'll have to use an assignment in the code block:



                                                                    if ($x == $y) { $var = "true"; }


                                                                    Alternatively, resort to a ?: ternary comparison.



                                                                    If in If



                                                                    You cannot nest an if within a condition either:




                                                                    if ($x == true and (if $y != false)) { ... }


                                                                    Which is obviously redundant, because the and (or or) already allows chaining comparisons.




                                                                  3. Forgotton ; semicolons



                                                                    Once more: Each control block needs to be a statement. If the previous code piece isn't terminated by a semicolon, then that's a guaranteed syntax error:




                                                                    $var = 1 + 2 + 3
                                                                    if (true) { … }


                                                                    Btw, the last line in a {…} code block needs a semicolon too.




                                                                  4. Semicolon too early



                                                                    Now it's probably wrong to blame a particular coding style, as this pitfall is too easy to overlook:




                                                                    if ($x == 5);
                                                                    {
                                                                    $y = 7;
                                                                    }
                                                                    else ←
                                                                    {
                                                                    $x = -1;
                                                                    }


                                                                    Which happens more often than you might imagine.




                                                                    • When you terminate the if () expression with ; it will execute a void statement. The ; becomes a an empty {} of its own!

                                                                    • The {…} block thus is detached from the if, and would always run.

                                                                    • So the else no longer had a relation to an open if construct,
                                                                      which is why this would lead to an Unexpected T_ELSE syntax error.





                                                                    Which also explains a likewise subtle variation of this syntax error:



                                                                    if ($x) { x_is_true(); }; else { something_else(); };


                                                                    Where the ; after the code block {…} terminates the whole if
                                                                    construct, severing the else branch syntactically.




                                                                  5. Not using code blocks



                                                                    It's syntactically allowed to omit curly braces {} for code blocks in if/elseif/else branches. Which sadly is a syntax style very common to unversed coders. (Under the false assumption this was quicker to type or read).



                                                                    However that's highly likely to trip up the syntax. Sooner or later additional statements will find their way into the if/else branches:



                                                                    if (true)
                                                                    $x = 5;
                                                                    elseif (false)
                                                                    $x = 6;
                                                                    $y = 7; ←
                                                                    else
                                                                    $z = 0;


                                                                    But to actually use code blocks, you do have to write {} them as such!




                                                                    Even seasoned programmers avoid this braceless syntax, or at least
                                                                    understand it as an exceptional exception to the rule.





                                                                  6. Else / Elseif in wrong order



                                                                    One thing to remind yourself is the conditional order, of course.



                                                                    if ($a) { … }
                                                                    else { … }
                                                                    elseif ($b) { … }



                                                                    You can have as many elseifs as you want, but else has to go last. That's just how it is.




                                                                  7. Class declarations



                                                                    As mentioned above, you can't have control statements in a class declaration:



                                                                    class xyz {
                                                                    if (true) {
                                                                    function ($var) {}
                                                                    }


                                                                    You either forgot a function definition, or closed one } too early in such cases.




                                                                  8. Unexpected T_ELSEIF / T_ELSE



                                                                    When mixing PHP and HTML, the closing } for an if/elseif must be in the same PHP block <?php ?> as the next elseif/else. This will generate an error as the closing } for the if needs to be part of the elseif:



                                                                    <?php if ($x) { ?>
                                                                    html
                                                                    <?php } ?>
                                                                    <?php elseif ($y) { ?>
                                                                    html
                                                                    <?php } ?>


                                                                    The correct form <?php } elseif:



                                                                    <?php if ($x) { ?>
                                                                    html
                                                                    <?php } elseif ($y) { ?>
                                                                    html
                                                                    <?php } ?>


                                                                    This is more or less a variation of incorrect indentation - presumably often based on wrong coding intentions.

                                                                    You cannot mash other statements inbetween if and elseif/else structural tokens:



                                                                    if (true) {
                                                                    }
                                                                    echo "in between"; ←
                                                                    elseif (false) {
                                                                    }
                                                                    ?> text <?php ←
                                                                    else {
                                                                    }


                                                                    Either can only occur in {…} code blocks, not in between control structure tokens.




                                                                    • This wouldn't make sense anyway. It's not like that there was some "undefined" state when PHP jumps between if and else branches.

                                                                    • You'll have to make up your mind where print statements belong to / or if they need to be repeated in both branches.





                                                                    Nor can you part an if/else between different control structures:



                                                                    foreach ($array as $i) {
                                                                    if ($i) { … }
                                                                    }
                                                                    else { … }


                                                                    There is no syntactic relation between the if and else. The foreach lexical scope ends at }, so there's no point for the if structure to continue.




                                                                  9. T_ENDIF



                                                                    If an unexpected T_ENDIF is complained about, you're using the alternative syntax style if:elseif:else:endif;. Which you should really think twice about.




                                                                    • A common pitfall is confusing the eerily similar : colon for a ; semicolon. (Covered in "Semicolon too early")


                                                                    • As indentation is harder to track in template files, the more when using the alternative syntax - it's plausible your endif; does not match any if:.


                                                                    • Using } endif;
                                                                      is a doubled if-terminator.




                                                                    While an "unexpected $end" is usually the price for a forgotten closing } curly brace.




                                                                  10. Assignment vs. comparison



                                                                    So, this is not a syntax error, but worth mentioning in this context:




                                                                    if ($x = true) { }
                                                                    else { do_false(); }


                                                                    That's not a ==/=== comparison, but an = assignment. This is rather subtle, and will easily lead some users to helplessly edit whole condition blocks. Watch out for unintended assignments first - whenver you experience a logic fault / misbeheviour.








                                                                  share|improve this answer




























                                                                    13












                                                                    13








                                                                    13








                                                                    Unexpected T_IF

                                                                    Unexpected T_ELSEIF

                                                                    Unexpected T_ELSE

                                                                    Unexpected T_ENDIF



                                                                    Conditional control blocks if, elseif and else follow a simple structure. When you encounter a syntax error, it's most likely just invalid block nesting → with missing { curly braces } - or one too many.



                                                                    enter image description here





                                                                    1. Missing { or } due to incorrect indentation



                                                                      Mismatched code braces are common to less well-formatted code such as:



                                                                      if((!($opt["uniQartz5.8"]!=$this->check58)) or (empty($_POST['poree']))) {if
                                                                      ($true) {echo"halp";} elseif((!$z)or%b){excSmthng(False,5.8)}elseif (False){


                                                                      If your code looks like this, start afresh! Otherwise it's unfixable to you or anyone else. There's no point in showcasing this on the internet to inquire for help.



                                                                      You will only be able to fix it, if you can visually follow the nested structure and relation of if/else conditionals and their { code blocks }. Use your IDE to see if they're all paired.



                                                                      if (true) {
                                                                      if (false) {

                                                                      }
                                                                      elseif ($whatever) {
                                                                      if ($something2) {

                                                                      }
                                                                      else {

                                                                      }
                                                                      }
                                                                      else {

                                                                      }
                                                                      if (false) { // a second `if` tree

                                                                      }
                                                                      else {

                                                                      }
                                                                      }
                                                                      elseif (false) {

                                                                      }


                                                                      Any double } } will not just close a branch, but a previous condition structure. Therefore stick with one coding style; don't mix and match in nested if/else trees.



                                                                      Apart from consistency here, it turns out helpful to avoid lengthy conditions too. Use temporary variables or functions to avoid unreadable if-expressions.




                                                                    2. IF cannot be used in expressions



                                                                      A surprisingly frequent newcomer mistake is trying to use an if statement in an expression, such as a print statement:




                                                                      echo "<a href='" . if ($link == "example.org") { echo …


                                                                      Which is invalid of course.



                                                                      You can use a ternary conditional, but beware of readability impacts.



                                                                      echo "<a href='" . ($link ? "http://yes" : "http://no") . "</a>";


                                                                      Otherwise break such output constructs up: use multiple ifs and echos.

                                                                      Better yet, use temporary variables, and place your conditionals before:



                                                                      if ($link) { $href = "yes"; } else { $href = "no"; }
                                                                      echo "<a href='$href'>Link</a>";


                                                                      Defining functions or methods for such cases often makes sense too.



                                                                      Control blocks don't return "results"



                                                                      Now this is less common, but a few coders even try to treat if as if it could return a result:



                                                                      $var = if ($x == $y) { "true" };


                                                                      Which is structurally identical to using if within a string concatenation / expression.




                                                                      • But control structures (if / foreach / while) don't have a "result".

                                                                      • The literal string "true" would also just be a void statement.





                                                                      You'll have to use an assignment in the code block:



                                                                      if ($x == $y) { $var = "true"; }


                                                                      Alternatively, resort to a ?: ternary comparison.



                                                                      If in If



                                                                      You cannot nest an if within a condition either:




                                                                      if ($x == true and (if $y != false)) { ... }


                                                                      Which is obviously redundant, because the and (or or) already allows chaining comparisons.




                                                                    3. Forgotton ; semicolons



                                                                      Once more: Each control block needs to be a statement. If the previous code piece isn't terminated by a semicolon, then that's a guaranteed syntax error:




                                                                      $var = 1 + 2 + 3
                                                                      if (true) { … }


                                                                      Btw, the last line in a {…} code block needs a semicolon too.




                                                                    4. Semicolon too early



                                                                      Now it's probably wrong to blame a particular coding style, as this pitfall is too easy to overlook:




                                                                      if ($x == 5);
                                                                      {
                                                                      $y = 7;
                                                                      }
                                                                      else ←
                                                                      {
                                                                      $x = -1;
                                                                      }


                                                                      Which happens more often than you might imagine.




                                                                      • When you terminate the if () expression with ; it will execute a void statement. The ; becomes a an empty {} of its own!

                                                                      • The {…} block thus is detached from the if, and would always run.

                                                                      • So the else no longer had a relation to an open if construct,
                                                                        which is why this would lead to an Unexpected T_ELSE syntax error.





                                                                      Which also explains a likewise subtle variation of this syntax error:



                                                                      if ($x) { x_is_true(); }; else { something_else(); };


                                                                      Where the ; after the code block {…} terminates the whole if
                                                                      construct, severing the else branch syntactically.




                                                                    5. Not using code blocks



                                                                      It's syntactically allowed to omit curly braces {} for code blocks in if/elseif/else branches. Which sadly is a syntax style very common to unversed coders. (Under the false assumption this was quicker to type or read).



                                                                      However that's highly likely to trip up the syntax. Sooner or later additional statements will find their way into the if/else branches:



                                                                      if (true)
                                                                      $x = 5;
                                                                      elseif (false)
                                                                      $x = 6;
                                                                      $y = 7; ←
                                                                      else
                                                                      $z = 0;


                                                                      But to actually use code blocks, you do have to write {} them as such!




                                                                      Even seasoned programmers avoid this braceless syntax, or at least
                                                                      understand it as an exceptional exception to the rule.





                                                                    6. Else / Elseif in wrong order



                                                                      One thing to remind yourself is the conditional order, of course.



                                                                      if ($a) { … }
                                                                      else { … }
                                                                      elseif ($b) { … }



                                                                      You can have as many elseifs as you want, but else has to go last. That's just how it is.




                                                                    7. Class declarations



                                                                      As mentioned above, you can't have control statements in a class declaration:



                                                                      class xyz {
                                                                      if (true) {
                                                                      function ($var) {}
                                                                      }


                                                                      You either forgot a function definition, or closed one } too early in such cases.




                                                                    8. Unexpected T_ELSEIF / T_ELSE



                                                                      When mixing PHP and HTML, the closing } for an if/elseif must be in the same PHP block <?php ?> as the next elseif/else. This will generate an error as the closing } for the if needs to be part of the elseif:



                                                                      <?php if ($x) { ?>
                                                                      html
                                                                      <?php } ?>
                                                                      <?php elseif ($y) { ?>
                                                                      html
                                                                      <?php } ?>


                                                                      The correct form <?php } elseif:



                                                                      <?php if ($x) { ?>
                                                                      html
                                                                      <?php } elseif ($y) { ?>
                                                                      html
                                                                      <?php } ?>


                                                                      This is more or less a variation of incorrect indentation - presumably often based on wrong coding intentions.

                                                                      You cannot mash other statements inbetween if and elseif/else structural tokens:



                                                                      if (true) {
                                                                      }
                                                                      echo "in between"; ←
                                                                      elseif (false) {
                                                                      }
                                                                      ?> text <?php ←
                                                                      else {
                                                                      }


                                                                      Either can only occur in {…} code blocks, not in between control structure tokens.




                                                                      • This wouldn't make sense anyway. It's not like that there was some "undefined" state when PHP jumps between if and else branches.

                                                                      • You'll have to make up your mind where print statements belong to / or if they need to be repeated in both branches.





                                                                      Nor can you part an if/else between different control structures:



                                                                      foreach ($array as $i) {
                                                                      if ($i) { … }
                                                                      }
                                                                      else { … }


                                                                      There is no syntactic relation between the if and else. The foreach lexical scope ends at }, so there's no point for the if structure to continue.




                                                                    9. T_ENDIF



                                                                      If an unexpected T_ENDIF is complained about, you're using the alternative syntax style if:elseif:else:endif;. Which you should really think twice about.




                                                                      • A common pitfall is confusing the eerily similar : colon for a ; semicolon. (Covered in "Semicolon too early")


                                                                      • As indentation is harder to track in template files, the more when using the alternative syntax - it's plausible your endif; does not match any if:.


                                                                      • Using } endif;
                                                                        is a doubled if-terminator.




                                                                      While an "unexpected $end" is usually the price for a forgotten closing } curly brace.




                                                                    10. Assignment vs. comparison



                                                                      So, this is not a syntax error, but worth mentioning in this context:




                                                                      if ($x = true) { }
                                                                      else { do_false(); }


                                                                      That's not a ==/=== comparison, but an = assignment. This is rather subtle, and will easily lead some users to helplessly edit whole condition blocks. Watch out for unintended assignments first - whenver you experience a logic fault / misbeheviour.








                                                                    share|improve this answer
















                                                                    Unexpected T_IF

                                                                    Unexpected T_ELSEIF

                                                                    Unexpected T_ELSE

                                                                    Unexpected T_ENDIF



                                                                    Conditional control blocks if, elseif and else follow a simple structure. When you encounter a syntax error, it's most likely just invalid block nesting → with missing { curly braces } - or one too many.



                                                                    enter image description here





                                                                    1. Missing { or } due to incorrect indentation



                                                                      Mismatched code braces are common to less well-formatted code such as:



                                                                      if((!($opt["uniQartz5.8"]!=$this->check58)) or (empty($_POST['poree']))) {if
                                                                      ($true) {echo"halp";} elseif((!$z)or%b){excSmthng(False,5.8)}elseif (False){


                                                                      If your code looks like this, start afresh! Otherwise it's unfixable to you or anyone else. There's no point in showcasing this on the internet to inquire for help.



                                                                      You will only be able to fix it, if you can visually follow the nested structure and relation of if/else conditionals and their { code blocks }. Use your IDE to see if they're all paired.



                                                                      if (true) {
                                                                      if (false) {

                                                                      }
                                                                      elseif ($whatever) {
                                                                      if ($something2) {

                                                                      }
                                                                      else {

                                                                      }
                                                                      }
                                                                      else {

                                                                      }
                                                                      if (false) { // a second `if` tree

                                                                      }
                                                                      else {

                                                                      }
                                                                      }
                                                                      elseif (false) {

                                                                      }


                                                                      Any double } } will not just close a branch, but a previous condition structure. Therefore stick with one coding style; don't mix and match in nested if/else trees.



                                                                      Apart from consistency here, it turns out helpful to avoid lengthy conditions too. Use temporary variables or functions to avoid unreadable if-expressions.




                                                                    2. IF cannot be used in expressions



                                                                      A surprisingly frequent newcomer mistake is trying to use an if statement in an expression, such as a print statement:




                                                                      echo "<a href='" . if ($link == "example.org") { echo …


                                                                      Which is invalid of course.



                                                                      You can use a ternary conditional, but beware of readability impacts.



                                                                      echo "<a href='" . ($link ? "http://yes" : "http://no") . "</a>";


                                                                      Otherwise break such output constructs up: use multiple ifs and echos.

                                                                      Better yet, use temporary variables, and place your conditionals before:



                                                                      if ($link) { $href = "yes"; } else { $href = "no"; }
                                                                      echo "<a href='$href'>Link</a>";


                                                                      Defining functions or methods for such cases often makes sense too.



                                                                      Control blocks don't return "results"



                                                                      Now this is less common, but a few coders even try to treat if as if it could return a result:



                                                                      $var = if ($x == $y) { "true" };


                                                                      Which is structurally identical to using if within a string concatenation / expression.




                                                                      • But control structures (if / foreach / while) don't have a "result".

                                                                      • The literal string "true" would also just be a void statement.





                                                                      You'll have to use an assignment in the code block:



                                                                      if ($x == $y) { $var = "true"; }


                                                                      Alternatively, resort to a ?: ternary comparison.



                                                                      If in If



                                                                      You cannot nest an if within a condition either:




                                                                      if ($x == true and (if $y != false)) { ... }


                                                                      Which is obviously redundant, because the and (or or) already allows chaining comparisons.




                                                                    3. Forgotton ; semicolons



                                                                      Once more: Each control block needs to be a statement. If the previous code piece isn't terminated by a semicolon, then that's a guaranteed syntax error:




                                                                      $var = 1 + 2 + 3
                                                                      if (true) { … }


                                                                      Btw, the last line in a {…} code block needs a semicolon too.




                                                                    4. Semicolon too early



                                                                      Now it's probably wrong to blame a particular coding style, as this pitfall is too easy to overlook:




                                                                      if ($x == 5);
                                                                      {
                                                                      $y = 7;
                                                                      }
                                                                      else ←
                                                                      {
                                                                      $x = -1;
                                                                      }


                                                                      Which happens more often than you might imagine.




                                                                      • When you terminate the if () expression with ; it will execute a void statement. The ; becomes a an empty {} of its own!

                                                                      • The {…} block thus is detached from the if, and would always run.

                                                                      • So the else no longer had a relation to an open if construct,
                                                                        which is why this would lead to an Unexpected T_ELSE syntax error.





                                                                      Which also explains a likewise subtle variation of this syntax error:



                                                                      if ($x) { x_is_true(); }; else { something_else(); };


                                                                      Where the ; after the code block {…} terminates the whole if
                                                                      construct, severing the else branch syntactically.




                                                                    5. Not using code blocks



                                                                      It's syntactically allowed to omit curly braces {} for code blocks in if/elseif/else branches. Which sadly is a syntax style very common to unversed coders. (Under the false assumption this was quicker to type or read).



                                                                      However that's highly likely to trip up the syntax. Sooner or later additional statements will find their way into the if/else branches:



                                                                      if (true)
                                                                      $x = 5;
                                                                      elseif (false)
                                                                      $x = 6;
                                                                      $y = 7; ←
                                                                      else
                                                                      $z = 0;


                                                                      But to actually use code blocks, you do have to write {} them as such!




                                                                      Even seasoned programmers avoid this braceless syntax, or at least
                                                                      understand it as an exceptional exception to the rule.





                                                                    6. Else / Elseif in wrong order



                                                                      One thing to remind yourself is the conditional order, of course.



                                                                      if ($a) { … }
                                                                      else { … }
                                                                      elseif ($b) { … }



                                                                      You can have as many elseifs as you want, but else has to go last. That's just how it is.




                                                                    7. Class declarations



                                                                      As mentioned above, you can't have control statements in a class declaration:



                                                                      class xyz {
                                                                      if (true) {
                                                                      function ($var) {}
                                                                      }


                                                                      You either forgot a function definition, or closed one } too early in such cases.




                                                                    8. Unexpected T_ELSEIF / T_ELSE



                                                                      When mixing PHP and HTML, the closing } for an if/elseif must be in the same PHP block <?php ?> as the next elseif/else. This will generate an error as the closing } for the if needs to be part of the elseif:



                                                                      <?php if ($x) { ?>
                                                                      html
                                                                      <?php } ?>
                                                                      <?php elseif ($y) { ?>
                                                                      html
                                                                      <?php } ?>


                                                                      The correct form <?php } elseif:



                                                                      <?php if ($x) { ?>
                                                                      html
                                                                      <?php } elseif ($y) { ?>
                                                                      html
                                                                      <?php } ?>


                                                                      This is more or less a variation of incorrect indentation - presumably often based on wrong coding intentions.

                                                                      You cannot mash other statements inbetween if and elseif/else structural tokens:



                                                                      if (true) {
                                                                      }
                                                                      echo "in between"; ←
                                                                      elseif (false) {
                                                                      }
                                                                      ?> text <?php ←
                                                                      else {
                                                                      }


                                                                      Either can only occur in {…} code blocks, not in between control structure tokens.




                                                                      • This wouldn't make sense anyway. It's not like that there was some "undefined" state when PHP jumps between if and else branches.

                                                                      • You'll have to make up your mind where print statements belong to / or if they need to be repeated in both branches.





                                                                      Nor can you part an if/else between different control structures:



                                                                      foreach ($array as $i) {
                                                                      if ($i) { … }
                                                                      }
                                                                      else { … }


                                                                      There is no syntactic relation between the if and else. The foreach lexical scope ends at }, so there's no point for the if structure to continue.




                                                                    9. T_ENDIF



                                                                      If an unexpected T_ENDIF is complained about, you're using the alternative syntax style if:elseif:else:endif;. Which you should really think twice about.




                                                                      • A common pitfall is confusing the eerily similar : colon for a ; semicolon. (Covered in "Semicolon too early")


                                                                      • As indentation is harder to track in template files, the more when using the alternative syntax - it's plausible your endif; does not match any if:.


                                                                      • Using } endif;
                                                                        is a doubled if-terminator.




                                                                      While an "unexpected $end" is usually the price for a forgotten closing } curly brace.




                                                                    10. Assignment vs. comparison



                                                                      So, this is not a syntax error, but worth mentioning in this context:




                                                                      if ($x = true) { }
                                                                      else { do_false(); }


                                                                      That's not a ==/=== comparison, but an = assignment. This is rather subtle, and will easily lead some users to helplessly edit whole condition blocks. Watch out for unintended assignments first - whenver you experience a logic fault / misbeheviour.









                                                                    share|improve this answer














                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Aug 10 '18 at 13:13


























                                                                    community wiki





                                                                    6 revs, 2 users 95%
                                                                    mario
























                                                                        11















                                                                        Unexpected T_IF

                                                                        Unexpected T_FOREACH

                                                                        Unexpected T_FOR

                                                                        Unexpected T_WHILE

                                                                        Unexpected T_DO

                                                                        Unexpected T_ECHO



                                                                        Control constructs such as if, foreach, for, while, list, global, return, do, print, echo may only be used as statements. They usually reside on a line by themselves.





                                                                        1. Semicolon; where you at?



                                                                          Pretty universally have you missed a semicolon in the previous line if the parser complains about a control statement:




                                                                          $x = myfunc()
                                                                          if (true) {


                                                                          Solution: look into the previous line; add semicolon.




                                                                        2. Class declarations



                                                                          Another location where this occurs is in class declarations. In the class section you can only list property initializations and method sections. No code may reside there.



                                                                          class xyz {
                                                                          if (true) {}
                                                                          foreach ($var) {}


                                                                          Such syntax errors commonly materialize for incorrectly nested { and }. In particular when function code blocks got closed too early.




                                                                        3. Statements in expression context



                                                                          Most language constructs can only be used as statements. They aren't meant to be placed inside other expressions:




                                                                          $var = array(1, 2, foreach($else as $_), 5, 6);


                                                                          Likewise can't you use an if in strings, math expressions or elsewhere:




                                                                          print "Oh, " . if (true) { "you!" } . " won't work";
                                                                          // Use a ternary condition here instead, when versed enough.


                                                                          For embedding if-like conditions in an expression specifically, you often want to use a ?: ternary evaluation.



                                                                          The same applies to for, while, global, echo and a lesser extend list.




                                                                          echo 123, echo 567, "huh?";


                                                                          Whereas print() is a language builtin that may be used in expression context. (But rarely makes sense.)




                                                                        4. Reserved keywords as identifiers



                                                                          You also can't use do or if and other language constructs for user-defined functions or class names. (Perhaps in PHP7. But even then it wouldn't be advisable.)








                                                                        share|improve this answer






























                                                                          11















                                                                          Unexpected T_IF

                                                                          Unexpected T_FOREACH

                                                                          Unexpected T_FOR

                                                                          Unexpected T_WHILE

                                                                          Unexpected T_DO

                                                                          Unexpected T_ECHO



                                                                          Control constructs such as if, foreach, for, while, list, global, return, do, print, echo may only be used as statements. They usually reside on a line by themselves.





                                                                          1. Semicolon; where you at?



                                                                            Pretty universally have you missed a semicolon in the previous line if the parser complains about a control statement:




                                                                            $x = myfunc()
                                                                            if (true) {


                                                                            Solution: look into the previous line; add semicolon.




                                                                          2. Class declarations



                                                                            Another location where this occurs is in class declarations. In the class section you can only list property initializations and method sections. No code may reside there.



                                                                            class xyz {
                                                                            if (true) {}
                                                                            foreach ($var) {}


                                                                            Such syntax errors commonly materialize for incorrectly nested { and }. In particular when function code blocks got closed too early.




                                                                          3. Statements in expression context



                                                                            Most language constructs can only be used as statements. They aren't meant to be placed inside other expressions:




                                                                            $var = array(1, 2, foreach($else as $_), 5, 6);


                                                                            Likewise can't you use an if in strings, math expressions or elsewhere:




                                                                            print "Oh, " . if (true) { "you!" } . " won't work";
                                                                            // Use a ternary condition here instead, when versed enough.


                                                                            For embedding if-like conditions in an expression specifically, you often want to use a ?: ternary evaluation.



                                                                            The same applies to for, while, global, echo and a lesser extend list.




                                                                            echo 123, echo 567, "huh?";


                                                                            Whereas print() is a language builtin that may be used in expression context. (But rarely makes sense.)




                                                                          4. Reserved keywords as identifiers



                                                                            You also can't use do or if and other language constructs for user-defined functions or class names. (Perhaps in PHP7. But even then it wouldn't be advisable.)








                                                                          share|improve this answer




























                                                                            11












                                                                            11








                                                                            11








                                                                            Unexpected T_IF

                                                                            Unexpected T_FOREACH

                                                                            Unexpected T_FOR

                                                                            Unexpected T_WHILE

                                                                            Unexpected T_DO

                                                                            Unexpected T_ECHO



                                                                            Control constructs such as if, foreach, for, while, list, global, return, do, print, echo may only be used as statements. They usually reside on a line by themselves.





                                                                            1. Semicolon; where you at?



                                                                              Pretty universally have you missed a semicolon in the previous line if the parser complains about a control statement:




                                                                              $x = myfunc()
                                                                              if (true) {


                                                                              Solution: look into the previous line; add semicolon.




                                                                            2. Class declarations



                                                                              Another location where this occurs is in class declarations. In the class section you can only list property initializations and method sections. No code may reside there.



                                                                              class xyz {
                                                                              if (true) {}
                                                                              foreach ($var) {}


                                                                              Such syntax errors commonly materialize for incorrectly nested { and }. In particular when function code blocks got closed too early.




                                                                            3. Statements in expression context



                                                                              Most language constructs can only be used as statements. They aren't meant to be placed inside other expressions:




                                                                              $var = array(1, 2, foreach($else as $_), 5, 6);


                                                                              Likewise can't you use an if in strings, math expressions or elsewhere:




                                                                              print "Oh, " . if (true) { "you!" } . " won't work";
                                                                              // Use a ternary condition here instead, when versed enough.


                                                                              For embedding if-like conditions in an expression specifically, you often want to use a ?: ternary evaluation.



                                                                              The same applies to for, while, global, echo and a lesser extend list.




                                                                              echo 123, echo 567, "huh?";


                                                                              Whereas print() is a language builtin that may be used in expression context. (But rarely makes sense.)




                                                                            4. Reserved keywords as identifiers



                                                                              You also can't use do or if and other language constructs for user-defined functions or class names. (Perhaps in PHP7. But even then it wouldn't be advisable.)








                                                                            share|improve this answer
















                                                                            Unexpected T_IF

                                                                            Unexpected T_FOREACH

                                                                            Unexpected T_FOR

                                                                            Unexpected T_WHILE

                                                                            Unexpected T_DO

                                                                            Unexpected T_ECHO



                                                                            Control constructs such as if, foreach, for, while, list, global, return, do, print, echo may only be used as statements. They usually reside on a line by themselves.





                                                                            1. Semicolon; where you at?



                                                                              Pretty universally have you missed a semicolon in the previous line if the parser complains about a control statement:




                                                                              $x = myfunc()
                                                                              if (true) {


                                                                              Solution: look into the previous line; add semicolon.




                                                                            2. Class declarations



                                                                              Another location where this occurs is in class declarations. In the class section you can only list property initializations and method sections. No code may reside there.



                                                                              class xyz {
                                                                              if (true) {}
                                                                              foreach ($var) {}


                                                                              Such syntax errors commonly materialize for incorrectly nested { and }. In particular when function code blocks got closed too early.




                                                                            3. Statements in expression context



                                                                              Most language constructs can only be used as statements. They aren't meant to be placed inside other expressions:




                                                                              $var = array(1, 2, foreach($else as $_), 5, 6);


                                                                              Likewise can't you use an if in strings, math expressions or elsewhere:




                                                                              print "Oh, " . if (true) { "you!" } . " won't work";
                                                                              // Use a ternary condition here instead, when versed enough.


                                                                              For embedding if-like conditions in an expression specifically, you often want to use a ?: ternary evaluation.



                                                                              The same applies to for, while, global, echo and a lesser extend list.




                                                                              echo 123, echo 567, "huh?";


                                                                              Whereas print() is a language builtin that may be used in expression context. (But rarely makes sense.)




                                                                            4. Reserved keywords as identifiers



                                                                              You also can't use do or if and other language constructs for user-defined functions or class names. (Perhaps in PHP7. But even then it wouldn't be advisable.)









                                                                            share|improve this answer














                                                                            share|improve this answer



                                                                            share|improve this answer








                                                                            edited May 23 '17 at 12:02


























                                                                            community wiki





                                                                            6 revs, 3 users 96%
                                                                            mario
























                                                                                10















                                                                                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 >



                                                                                Comparison operators such as ==, >=, ===, !=, <>, !== and <= or < and > mostly should be used just in expressions, such as if expressions. If the parser complains about them, then it often means incorrect paring or mismatched ( ) parens around them.





                                                                                1. Parens grouping



                                                                                  In particular for if statements with multiple comparisons you must take care to correctly count opening and closing parenthesis:




                                                                                  if (($foo < 7) && $bar) > 5 || $baz < 9) { ... }



                                                                                  Here the if condition here was already terminated by the )



                                                                                  Once your comparisons become sufficiently complex it often helps to split it up into multiple and nested if constructs rather.




                                                                                2. isset() mashed with comparing



                                                                                  A common newcomer is pitfal is trying to combine isset() or empty() with comparisons:




                                                                                  if (empty($_POST["var"] == 1)) {


                                                                                  Or even:




                                                                                  if (isset($variable !== "value")) {


                                                                                  This doesn't make sense to PHP, because isset and empty are language constructs that only accept variable names. It doesn't make sense to compare the result either, because the output is only/already a boolean.




                                                                                3. Confusing >= greater-or-equal with => array operator



                                                                                  Both operators look somewhat similar, so they sometimes get mixed up:




                                                                                  if ($var => 5) { ... }


                                                                                  You only need to remember that this comparison operator is called "greater than or equal" to get it right.



                                                                                  See also: If statement structure in PHP




                                                                                4. Nothing to compare against



                                                                                  You also can't combine two comparisons if they pertain the same variable name:




                                                                                  if ($xyz > 5 and < 100)


                                                                                  PHP can't deduce that you meant to compare the initial variable again. Expressions are usually paired according to operator precedence, so by the time the < is seen, there'd be only a boolean result left from the original variable.



                                                                                  See also: unexpected T_IS_SMALLER_OR_EQUAL




                                                                                5. Comparison chains



                                                                                  You can't compare against a variable with a row of operators:




                                                                                  $reult = (5 < $x < 10);


                                                                                  This has to be broken up into two comparisons, each against $x.



                                                                                  This is actually more a case of blacklisted expressions (due to equivalent operator associativity). It's syntactically valid in a few C-style languages, but PHP wouldn't interpret it as expected comparison chain either.




                                                                                6. Unexpected >
                                                                                  Unexpected <



                                                                                  The greater than > or less than < operators don't have a custom T_XXX tokenizer name. And while they can be misplaced like all they others, you more often see the parser complain about them for misquoted strings and mashed HTML:




                                                                                  print "<a href='z">Hello</a>";



                                                                                  This amounts to a string "<a href='z" being compared > to a literal constant Hello and then another < comparison. Or that's at least how PHP sees it. The actual cause and syntax mistake was the premature string " termination.



                                                                                  It's also not possible to nest PHP start tags:



                                                                                  <?php echo <?php my_func(); ?>




                                                                                See also:




                                                                                • php unexpected T_IS_NOT_EQUAL error

                                                                                • syntax error, unexpected T_IS_EQUAL

                                                                                • Syntax error on return statement

                                                                                • http://forums.phpfreaks.com/topic/96891-parse-error-syntax-error-unexpected-t-is-not-identical-expecting-or/






                                                                                share|improve this answer






























                                                                                  10















                                                                                  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 >



                                                                                  Comparison operators such as ==, >=, ===, !=, <>, !== and <= or < and > mostly should be used just in expressions, such as if expressions. If the parser complains about them, then it often means incorrect paring or mismatched ( ) parens around them.





                                                                                  1. Parens grouping



                                                                                    In particular for if statements with multiple comparisons you must take care to correctly count opening and closing parenthesis:




                                                                                    if (($foo < 7) && $bar) > 5 || $baz < 9) { ... }



                                                                                    Here the if condition here was already terminated by the )



                                                                                    Once your comparisons become sufficiently complex it often helps to split it up into multiple and nested if constructs rather.




                                                                                  2. isset() mashed with comparing



                                                                                    A common newcomer is pitfal is trying to combine isset() or empty() with comparisons:




                                                                                    if (empty($_POST["var"] == 1)) {


                                                                                    Or even:




                                                                                    if (isset($variable !== "value")) {


                                                                                    This doesn't make sense to PHP, because isset and empty are language constructs that only accept variable names. It doesn't make sense to compare the result either, because the output is only/already a boolean.




                                                                                  3. Confusing >= greater-or-equal with => array operator



                                                                                    Both operators look somewhat similar, so they sometimes get mixed up:




                                                                                    if ($var => 5) { ... }


                                                                                    You only need to remember that this comparison operator is called "greater than or equal" to get it right.



                                                                                    See also: If statement structure in PHP




                                                                                  4. Nothing to compare against



                                                                                    You also can't combine two comparisons if they pertain the same variable name:




                                                                                    if ($xyz > 5 and < 100)


                                                                                    PHP can't deduce that you meant to compare the initial variable again. Expressions are usually paired according to operator precedence, so by the time the < is seen, there'd be only a boolean result left from the original variable.



                                                                                    See also: unexpected T_IS_SMALLER_OR_EQUAL




                                                                                  5. Comparison chains



                                                                                    You can't compare against a variable with a row of operators:




                                                                                    $reult = (5 < $x < 10);


                                                                                    This has to be broken up into two comparisons, each against $x.



                                                                                    This is actually more a case of blacklisted expressions (due to equivalent operator associativity). It's syntactically valid in a few C-style languages, but PHP wouldn't interpret it as expected comparison chain either.




                                                                                  6. Unexpected >
                                                                                    Unexpected <



                                                                                    The greater than > or less than < operators don't have a custom T_XXX tokenizer name. And while they can be misplaced like all they others, you more often see the parser complain about them for misquoted strings and mashed HTML:




                                                                                    print "<a href='z">Hello</a>";



                                                                                    This amounts to a string "<a href='z" being compared > to a literal constant Hello and then another < comparison. Or that's at least how PHP sees it. The actual cause and syntax mistake was the premature string " termination.



                                                                                    It's also not possible to nest PHP start tags:



                                                                                    <?php echo <?php my_func(); ?>




                                                                                  See also:




                                                                                  • php unexpected T_IS_NOT_EQUAL error

                                                                                  • syntax error, unexpected T_IS_EQUAL

                                                                                  • Syntax error on return statement

                                                                                  • http://forums.phpfreaks.com/topic/96891-parse-error-syntax-error-unexpected-t-is-not-identical-expecting-or/






                                                                                  share|improve this answer




























                                                                                    10












                                                                                    10








                                                                                    10








                                                                                    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 >



                                                                                    Comparison operators such as ==, >=, ===, !=, <>, !== and <= or < and > mostly should be used just in expressions, such as if expressions. If the parser complains about them, then it often means incorrect paring or mismatched ( ) parens around them.





                                                                                    1. Parens grouping



                                                                                      In particular for if statements with multiple comparisons you must take care to correctly count opening and closing parenthesis:




                                                                                      if (($foo < 7) && $bar) > 5 || $baz < 9) { ... }



                                                                                      Here the if condition here was already terminated by the )



                                                                                      Once your comparisons become sufficiently complex it often helps to split it up into multiple and nested if constructs rather.




                                                                                    2. isset() mashed with comparing



                                                                                      A common newcomer is pitfal is trying to combine isset() or empty() with comparisons:




                                                                                      if (empty($_POST["var"] == 1)) {


                                                                                      Or even:




                                                                                      if (isset($variable !== "value")) {


                                                                                      This doesn't make sense to PHP, because isset and empty are language constructs that only accept variable names. It doesn't make sense to compare the result either, because the output is only/already a boolean.




                                                                                    3. Confusing >= greater-or-equal with => array operator



                                                                                      Both operators look somewhat similar, so they sometimes get mixed up:




                                                                                      if ($var => 5) { ... }


                                                                                      You only need to remember that this comparison operator is called "greater than or equal" to get it right.



                                                                                      See also: If statement structure in PHP




                                                                                    4. Nothing to compare against



                                                                                      You also can't combine two comparisons if they pertain the same variable name:




                                                                                      if ($xyz > 5 and < 100)


                                                                                      PHP can't deduce that you meant to compare the initial variable again. Expressions are usually paired according to operator precedence, so by the time the < is seen, there'd be only a boolean result left from the original variable.



                                                                                      See also: unexpected T_IS_SMALLER_OR_EQUAL




                                                                                    5. Comparison chains



                                                                                      You can't compare against a variable with a row of operators:




                                                                                      $reult = (5 < $x < 10);


                                                                                      This has to be broken up into two comparisons, each against $x.



                                                                                      This is actually more a case of blacklisted expressions (due to equivalent operator associativity). It's syntactically valid in a few C-style languages, but PHP wouldn't interpret it as expected comparison chain either.




                                                                                    6. Unexpected >
                                                                                      Unexpected <



                                                                                      The greater than > or less than < operators don't have a custom T_XXX tokenizer name. And while they can be misplaced like all they others, you more often see the parser complain about them for misquoted strings and mashed HTML:




                                                                                      print "<a href='z">Hello</a>";



                                                                                      This amounts to a string "<a href='z" being compared > to a literal constant Hello and then another < comparison. Or that's at least how PHP sees it. The actual cause and syntax mistake was the premature string " termination.



                                                                                      It's also not possible to nest PHP start tags:



                                                                                      <?php echo <?php my_func(); ?>




                                                                                    See also:




                                                                                    • php unexpected T_IS_NOT_EQUAL error

                                                                                    • syntax error, unexpected T_IS_EQUAL

                                                                                    • Syntax error on return statement

                                                                                    • http://forums.phpfreaks.com/topic/96891-parse-error-syntax-error-unexpected-t-is-not-identical-expecting-or/






                                                                                    share|improve this answer
















                                                                                    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 >



                                                                                    Comparison operators such as ==, >=, ===, !=, <>, !== and <= or < and > mostly should be used just in expressions, such as if expressions. If the parser complains about them, then it often means incorrect paring or mismatched ( ) parens around them.





                                                                                    1. Parens grouping



                                                                                      In particular for if statements with multiple comparisons you must take care to correctly count opening and closing parenthesis:




                                                                                      if (($foo < 7) && $bar) > 5 || $baz < 9) { ... }



                                                                                      Here the if condition here was already terminated by the )



                                                                                      Once your comparisons become sufficiently complex it often helps to split it up into multiple and nested if constructs rather.




                                                                                    2. isset() mashed with comparing



                                                                                      A common newcomer is pitfal is trying to combine isset() or empty() with comparisons:




                                                                                      if (empty($_POST["var"] == 1)) {


                                                                                      Or even:




                                                                                      if (isset($variable !== "value")) {


                                                                                      This doesn't make sense to PHP, because isset and empty are language constructs that only accept variable names. It doesn't make sense to compare the result either, because the output is only/already a boolean.




                                                                                    3. Confusing >= greater-or-equal with => array operator



                                                                                      Both operators look somewhat similar, so they sometimes get mixed up:




                                                                                      if ($var => 5) { ... }


                                                                                      You only need to remember that this comparison operator is called "greater than or equal" to get it right.



                                                                                      See also: If statement structure in PHP




                                                                                    4. Nothing to compare against



                                                                                      You also can't combine two comparisons if they pertain the same variable name:




                                                                                      if ($xyz > 5 and < 100)


                                                                                      PHP can't deduce that you meant to compare the initial variable again. Expressions are usually paired according to operator precedence, so by the time the < is seen, there'd be only a boolean result left from the original variable.



                                                                                      See also: unexpected T_IS_SMALLER_OR_EQUAL




                                                                                    5. Comparison chains



                                                                                      You can't compare against a variable with a row of operators:




                                                                                      $reult = (5 < $x < 10);


                                                                                      This has to be broken up into two comparisons, each against $x.



                                                                                      This is actually more a case of blacklisted expressions (due to equivalent operator associativity). It's syntactically valid in a few C-style languages, but PHP wouldn't interpret it as expected comparison chain either.




                                                                                    6. Unexpected >
                                                                                      Unexpected <



                                                                                      The greater than > or less than < operators don't have a custom T_XXX tokenizer name. And while they can be misplaced like all they others, you more often see the parser complain about them for misquoted strings and mashed HTML:




                                                                                      print "<a href='z">Hello</a>";



                                                                                      This amounts to a string "<a href='z" being compared > to a literal constant Hello and then another < comparison. Or that's at least how PHP sees it. The actual cause and syntax mistake was the premature string " termination.



                                                                                      It's also not possible to nest PHP start tags:



                                                                                      <?php echo <?php my_func(); ?>




                                                                                    See also:




                                                                                    • php unexpected T_IS_NOT_EQUAL error

                                                                                    • syntax error, unexpected T_IS_EQUAL

                                                                                    • Syntax error on return statement

                                                                                    • http://forums.phpfreaks.com/topic/96891-parse-error-syntax-error-unexpected-t-is-not-identical-expecting-or/







                                                                                    share|improve this answer














                                                                                    share|improve this answer



                                                                                    share|improve this answer








                                                                                    edited Oct 29 '18 at 4:05


























                                                                                    community wiki





                                                                                    4 revs
                                                                                    mario
























                                                                                        5














                                                                                        Unexpected T_LNUMBER



                                                                                        The token T_LNUMBER refers to a "long" / number.





                                                                                        1. Invalid variable names



                                                                                          In PHP, and most other programming languages, variables cannot start with a number. The first character must be alphabetic or an underscore.



                                                                                          $1   // Bad
                                                                                          $_1 // Good


                                                                                          *





                                                                                          • Quite often comes up for using preg_replace-placeholders "$1" in PHP context:



                                                                                            #                         ↓            ⇓  ↓
                                                                                            preg_replace("/#(w+)/e", strtopupper($1) )


                                                                                            Where the callback should have been quoted. (Now the /e regex flag has been deprecated. But it's sometimes still misused in preg_replace_callback functions.)




                                                                                          • The same identifier constraint applies to object properties, btw.




                                                                                            $json->0->value


                                                                                          • While the tokenizer/parser does not allow a literal $1 as variable name, one could use ${1} or ${"1"}. Which is a syntactic workaround for non-standard identifiers. (It's best to think of it as a local scope lookup. But generally: prefer plain arrays for such cases!)


                                                                                          • Amusingly, but very much not recommended, PHPs parser allows Unicode-identifiers; such that $➊ would be valid. (Unlike a literal 1).





                                                                                        2. Stray array entry



                                                                                          An unexpected long can also occur for array declarations - when missing , commas:



                                                                                          #            ↓ ↓
                                                                                          $xy = array(1 2 3);


                                                                                          Or likewise function calls and declarations, and other constructs:




                                                                                          • func(1, 2 3);

                                                                                          • function xy($z 2);


                                                                                          • for ($i=2 3<$z)




                                                                                          So usually there's one of ; or , missing for separating lists or expressions.




                                                                                        3. Misquoted HTML



                                                                                          And again, misquoted strings are a frequent source of stray numbers:



                                                                                          #                 ↓ ↓          
                                                                                          echo "<td colspan="3">something bad</td>";


                                                                                          Such cases should be treated more or less like Unexpected T_STRING errors.




                                                                                        4. Other identifiers



                                                                                          Neither functions, classes, nor namespaces can be named beginning with a number either:




                                                                                          function 123shop() {


                                                                                          Pretty much the same as for variable names.








                                                                                        share|improve this answer






























                                                                                          5














                                                                                          Unexpected T_LNUMBER



                                                                                          The token T_LNUMBER refers to a "long" / number.





                                                                                          1. Invalid variable names



                                                                                            In PHP, and most other programming languages, variables cannot start with a number. The first character must be alphabetic or an underscore.



                                                                                            $1   // Bad
                                                                                            $_1 // Good


                                                                                            *





                                                                                            • Quite often comes up for using preg_replace-placeholders "$1" in PHP context:



                                                                                              #                         ↓            ⇓  ↓
                                                                                              preg_replace("/#(w+)/e", strtopupper($1) )


                                                                                              Where the callback should have been quoted. (Now the /e regex flag has been deprecated. But it's sometimes still misused in preg_replace_callback functions.)




                                                                                            • The same identifier constraint applies to object properties, btw.




                                                                                              $json->0->value


                                                                                            • While the tokenizer/parser does not allow a literal $1 as variable name, one could use ${1} or ${"1"}. Which is a syntactic workaround for non-standard identifiers. (It's best to think of it as a local scope lookup. But generally: prefer plain arrays for such cases!)


                                                                                            • Amusingly, but very much not recommended, PHPs parser allows Unicode-identifiers; such that $➊ would be valid. (Unlike a literal 1).





                                                                                          2. Stray array entry



                                                                                            An unexpected long can also occur for array declarations - when missing , commas:



                                                                                            #            ↓ ↓
                                                                                            $xy = array(1 2 3);


                                                                                            Or likewise function calls and declarations, and other constructs:




                                                                                            • func(1, 2 3);

                                                                                            • function xy($z 2);


                                                                                            • for ($i=2 3<$z)




                                                                                            So usually there's one of ; or , missing for separating lists or expressions.




                                                                                          3. Misquoted HTML



                                                                                            And again, misquoted strings are a frequent source of stray numbers:



                                                                                            #                 ↓ ↓          
                                                                                            echo "<td colspan="3">something bad</td>";


                                                                                            Such cases should be treated more or less like Unexpected T_STRING errors.




                                                                                          4. Other identifiers



                                                                                            Neither functions, classes, nor namespaces can be named beginning with a number either:




                                                                                            function 123shop() {


                                                                                            Pretty much the same as for variable names.








                                                                                          share|improve this answer




























                                                                                            5












                                                                                            5








                                                                                            5







                                                                                            Unexpected T_LNUMBER



                                                                                            The token T_LNUMBER refers to a "long" / number.





                                                                                            1. Invalid variable names



                                                                                              In PHP, and most other programming languages, variables cannot start with a number. The first character must be alphabetic or an underscore.



                                                                                              $1   // Bad
                                                                                              $_1 // Good


                                                                                              *





                                                                                              • Quite often comes up for using preg_replace-placeholders "$1" in PHP context:



                                                                                                #                         ↓            ⇓  ↓
                                                                                                preg_replace("/#(w+)/e", strtopupper($1) )


                                                                                                Where the callback should have been quoted. (Now the /e regex flag has been deprecated. But it's sometimes still misused in preg_replace_callback functions.)




                                                                                              • The same identifier constraint applies to object properties, btw.




                                                                                                $json->0->value


                                                                                              • While the tokenizer/parser does not allow a literal $1 as variable name, one could use ${1} or ${"1"}. Which is a syntactic workaround for non-standard identifiers. (It's best to think of it as a local scope lookup. But generally: prefer plain arrays for such cases!)


                                                                                              • Amusingly, but very much not recommended, PHPs parser allows Unicode-identifiers; such that $➊ would be valid. (Unlike a literal 1).





                                                                                            2. Stray array entry



                                                                                              An unexpected long can also occur for array declarations - when missing , commas:



                                                                                              #            ↓ ↓
                                                                                              $xy = array(1 2 3);


                                                                                              Or likewise function calls and declarations, and other constructs:




                                                                                              • func(1, 2 3);

                                                                                              • function xy($z 2);


                                                                                              • for ($i=2 3<$z)




                                                                                              So usually there's one of ; or , missing for separating lists or expressions.




                                                                                            3. Misquoted HTML



                                                                                              And again, misquoted strings are a frequent source of stray numbers:



                                                                                              #                 ↓ ↓          
                                                                                              echo "<td colspan="3">something bad</td>";


                                                                                              Such cases should be treated more or less like Unexpected T_STRING errors.




                                                                                            4. Other identifiers



                                                                                              Neither functions, classes, nor namespaces can be named beginning with a number either:




                                                                                              function 123shop() {


                                                                                              Pretty much the same as for variable names.








                                                                                            share|improve this answer















                                                                                            Unexpected T_LNUMBER



                                                                                            The token T_LNUMBER refers to a "long" / number.





                                                                                            1. Invalid variable names



                                                                                              In PHP, and most other programming languages, variables cannot start with a number. The first character must be alphabetic or an underscore.



                                                                                              $1   // Bad
                                                                                              $_1 // Good


                                                                                              *





                                                                                              • Quite often comes up for using preg_replace-placeholders "$1" in PHP context:



                                                                                                #                         ↓            ⇓  ↓
                                                                                                preg_replace("/#(w+)/e", strtopupper($1) )


                                                                                                Where the callback should have been quoted. (Now the /e regex flag has been deprecated. But it's sometimes still misused in preg_replace_callback functions.)




                                                                                              • The same identifier constraint applies to object properties, btw.




                                                                                                $json->0->value


                                                                                              • While the tokenizer/parser does not allow a literal $1 as variable name, one could use ${1} or ${"1"}. Which is a syntactic workaround for non-standard identifiers. (It's best to think of it as a local scope lookup. But generally: prefer plain arrays for such cases!)


                                                                                              • Amusingly, but very much not recommended, PHPs parser allows Unicode-identifiers; such that $➊ would be valid. (Unlike a literal 1).





                                                                                            2. Stray array entry



                                                                                              An unexpected long can also occur for array declarations - when missing , commas:



                                                                                              #            ↓ ↓
                                                                                              $xy = array(1 2 3);


                                                                                              Or likewise function calls and declarations, and other constructs:




                                                                                              • func(1, 2 3);

                                                                                              • function xy($z 2);


                                                                                              • for ($i=2 3<$z)




                                                                                              So usually there's one of ; or , missing for separating lists or expressions.




                                                                                            3. Misquoted HTML



                                                                                              And again, misquoted strings are a frequent source of stray numbers:



                                                                                              #                 ↓ ↓          
                                                                                              echo "<td colspan="3">something bad</td>";


                                                                                              Such cases should be treated more or less like Unexpected T_STRING errors.




                                                                                            4. Other identifiers



                                                                                              Neither functions, classes, nor namespaces can be named beginning with a number either:




                                                                                              function 123shop() {


                                                                                              Pretty much the same as for variable names.









                                                                                            share|improve this answer














                                                                                            share|improve this answer



                                                                                            share|improve this answer








                                                                                            edited Aug 10 '18 at 19:47


























                                                                                            community wiki





                                                                                            3 revs, 3 users 82%
                                                                                            mario
























                                                                                                5














                                                                                                Unexpected '?'



                                                                                                If you are trying to use the null coalescing operator ?? in a version of PHP prior to PHP 7 you will get this error.



                                                                                                <?= $a ?? 2; // works in PHP 7+
                                                                                                <?= (!empty($a)) ? $a : 2; // All versions of PHP


                                                                                                Unexpected '?', expecting variable



                                                                                                A similar error can occur for nullable types, as in:



                                                                                                function add(?int $sum): ?int {


                                                                                                Which again indicates an outdated PHP version being used (either the CLI version php -v or the webserver bound one phpinfo();).






                                                                                                share|improve this answer






























                                                                                                  5














                                                                                                  Unexpected '?'



                                                                                                  If you are trying to use the null coalescing operator ?? in a version of PHP prior to PHP 7 you will get this error.



                                                                                                  <?= $a ?? 2; // works in PHP 7+
                                                                                                  <?= (!empty($a)) ? $a : 2; // All versions of PHP


                                                                                                  Unexpected '?', expecting variable



                                                                                                  A similar error can occur for nullable types, as in:



                                                                                                  function add(?int $sum): ?int {


                                                                                                  Which again indicates an outdated PHP version being used (either the CLI version php -v or the webserver bound one phpinfo();).






                                                                                                  share|improve this answer




























                                                                                                    5












                                                                                                    5








                                                                                                    5







                                                                                                    Unexpected '?'



                                                                                                    If you are trying to use the null coalescing operator ?? in a version of PHP prior to PHP 7 you will get this error.



                                                                                                    <?= $a ?? 2; // works in PHP 7+
                                                                                                    <?= (!empty($a)) ? $a : 2; // All versions of PHP


                                                                                                    Unexpected '?', expecting variable



                                                                                                    A similar error can occur for nullable types, as in:



                                                                                                    function add(?int $sum): ?int {


                                                                                                    Which again indicates an outdated PHP version being used (either the CLI version php -v or the webserver bound one phpinfo();).






                                                                                                    share|improve this answer















                                                                                                    Unexpected '?'



                                                                                                    If you are trying to use the null coalescing operator ?? in a version of PHP prior to PHP 7 you will get this error.



                                                                                                    <?= $a ?? 2; // works in PHP 7+
                                                                                                    <?= (!empty($a)) ? $a : 2; // All versions of PHP


                                                                                                    Unexpected '?', expecting variable



                                                                                                    A similar error can occur for nullable types, as in:



                                                                                                    function add(?int $sum): ?int {


                                                                                                    Which again indicates an outdated PHP version being used (either the CLI version php -v or the webserver bound one phpinfo();).







                                                                                                    share|improve this answer














                                                                                                    share|improve this answer



                                                                                                    share|improve this answer








                                                                                                    edited Jan 9 at 14:20


























                                                                                                    community wiki





                                                                                                    2 revs, 2 users 56%
                                                                                                    John Conde
























                                                                                                        2














                                                                                                        Unexpected '='



                                                                                                        This can be caused by having invalid characters in a variable name. Variables names must follow these rules:




                                                                                                        Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*'







                                                                                                        share|improve this answer


























                                                                                                        • Good addition John.

                                                                                                          – Funk Forty Niner
                                                                                                          Dec 19 '18 at 3:02
















                                                                                                        2














                                                                                                        Unexpected '='



                                                                                                        This can be caused by having invalid characters in a variable name. Variables names must follow these rules:




                                                                                                        Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*'







                                                                                                        share|improve this answer


























                                                                                                        • Good addition John.

                                                                                                          – Funk Forty Niner
                                                                                                          Dec 19 '18 at 3:02














                                                                                                        2












                                                                                                        2








                                                                                                        2







                                                                                                        Unexpected '='



                                                                                                        This can be caused by having invalid characters in a variable name. Variables names must follow these rules:




                                                                                                        Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*'







                                                                                                        share|improve this answer















                                                                                                        Unexpected '='



                                                                                                        This can be caused by having invalid characters in a variable name. Variables names must follow these rules:




                                                                                                        Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*'








                                                                                                        share|improve this answer














                                                                                                        share|improve this answer



                                                                                                        share|improve this answer








                                                                                                        answered Oct 29 '18 at 2:22


























                                                                                                        community wiki





                                                                                                        John Conde














                                                                                                        • Good addition John.

                                                                                                          – Funk Forty Niner
                                                                                                          Dec 19 '18 at 3:02



















                                                                                                        • Good addition John.

                                                                                                          – Funk Forty Niner
                                                                                                          Dec 19 '18 at 3:02

















                                                                                                        Good addition John.

                                                                                                        – Funk Forty Niner
                                                                                                        Dec 19 '18 at 3:02





                                                                                                        Good addition John.

                                                                                                        – Funk Forty Niner
                                                                                                        Dec 19 '18 at 3:02











                                                                                                        1














                                                                                                        Unexpected 'continue' (T_CONTINUE)



                                                                                                        continue is a statement (like for, or if) and must appear standalone. It cannot be used as part of an expression. Partly because continue doesn't return a value, but in an expression every sub-expression must result in some value so the overall expression results in a value. That's the difference between a statement and an expression.



                                                                                                        That means continue cannot be used in a ternary statement or any statement that requires a return value.



                                                                                                        Unexpected 'break' (T_BREAK)



                                                                                                        Same goes for break; of course. It's also not usable in expression context, but a strict statement (on the same level as foreach or an if block).



                                                                                                        Unexpected 'return' (T_RETURN)



                                                                                                        Now this might be more surprising for return, but that's also just a block-level statement. It does return a value (or NULL) to the higher scope/function, but it does not evaluate as expression itself. → That is: there's no point in doing return(return(false);;






                                                                                                        share|improve this answer






























                                                                                                          1














                                                                                                          Unexpected 'continue' (T_CONTINUE)



                                                                                                          continue is a statement (like for, or if) and must appear standalone. It cannot be used as part of an expression. Partly because continue doesn't return a value, but in an expression every sub-expression must result in some value so the overall expression results in a value. That's the difference between a statement and an expression.



                                                                                                          That means continue cannot be used in a ternary statement or any statement that requires a return value.



                                                                                                          Unexpected 'break' (T_BREAK)



                                                                                                          Same goes for break; of course. It's also not usable in expression context, but a strict statement (on the same level as foreach or an if block).



                                                                                                          Unexpected 'return' (T_RETURN)



                                                                                                          Now this might be more surprising for return, but that's also just a block-level statement. It does return a value (or NULL) to the higher scope/function, but it does not evaluate as expression itself. → That is: there's no point in doing return(return(false);;






                                                                                                          share|improve this answer




























                                                                                                            1












                                                                                                            1








                                                                                                            1







                                                                                                            Unexpected 'continue' (T_CONTINUE)



                                                                                                            continue is a statement (like for, or if) and must appear standalone. It cannot be used as part of an expression. Partly because continue doesn't return a value, but in an expression every sub-expression must result in some value so the overall expression results in a value. That's the difference between a statement and an expression.



                                                                                                            That means continue cannot be used in a ternary statement or any statement that requires a return value.



                                                                                                            Unexpected 'break' (T_BREAK)



                                                                                                            Same goes for break; of course. It's also not usable in expression context, but a strict statement (on the same level as foreach or an if block).



                                                                                                            Unexpected 'return' (T_RETURN)



                                                                                                            Now this might be more surprising for return, but that's also just a block-level statement. It does return a value (or NULL) to the higher scope/function, but it does not evaluate as expression itself. → That is: there's no point in doing return(return(false);;






                                                                                                            share|improve this answer















                                                                                                            Unexpected 'continue' (T_CONTINUE)



                                                                                                            continue is a statement (like for, or if) and must appear standalone. It cannot be used as part of an expression. Partly because continue doesn't return a value, but in an expression every sub-expression must result in some value so the overall expression results in a value. That's the difference between a statement and an expression.



                                                                                                            That means continue cannot be used in a ternary statement or any statement that requires a return value.



                                                                                                            Unexpected 'break' (T_BREAK)



                                                                                                            Same goes for break; of course. It's also not usable in expression context, but a strict statement (on the same level as foreach or an if block).



                                                                                                            Unexpected 'return' (T_RETURN)



                                                                                                            Now this might be more surprising for return, but that's also just a block-level statement. It does return a value (or NULL) to the higher scope/function, but it does not evaluate as expression itself. → That is: there's no point in doing return(return(false);;







                                                                                                            share|improve this answer














                                                                                                            share|improve this answer



                                                                                                            share|improve this answer








                                                                                                            edited Aug 10 '18 at 13:10


























                                                                                                            community wiki





                                                                                                            2 revs, 2 users 53%
                                                                                                            mario
















                                                                                                                Popular posts from this blog

                                                                                                                Florida Star v. B. J. F.

                                                                                                                Danny Elfman

                                                                                                                Lugert, Oklahoma