Skip to main content
The 2024 Developer Survey results are live! See the results
added 352 characters in body
Source Link
mdfst13
  • 880
  • 8
  • 20

When PHP talks about an "unexpected $end", it means that your code ended prematurelywhile the parser is expecting more code. (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.)

It's pretty much always about a missing } curly brace to close preceding code blocks. What it's saying is that the parser is expecting to find a closing } but actually reached the end of the file.

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

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. Particularly after you find yourself adding more statements at the end of the script.

Very common error when specifiyingspecifying paths in Windows: "C:\xampp\htdocs\" is wrong. You need "C:\\xampp\\htdocs\\". Alternately, PHP will usually convert Unix-style paths (e.g. "C:/xampp/htdocs/") to the correct path for Windows.

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

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

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

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.

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

When PHP talks about an "unexpected $end", it means that your code ended while the parser is expecting more code. (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.)

It's pretty much always about a missing } curly brace to close preceding code blocks. What it's saying is that the parser is expecting to find a closing } but actually reached the end of the file.

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

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. Particularly after you find yourself adding more statements at the end of the script.

Very common error when specifying paths in Windows: "C:\xampp\htdocs\" is wrong. You need "C:\\xampp\\htdocs\\". Alternately, PHP will usually convert Unix-style paths (e.g. "C:/xampp/htdocs/") to the correct path for Windows.

Added a note about unescaped quotation marks.
Source Link
Robbie
  • 17.7k
  • 4
  • 36
  • 74

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:\xampp\htdocs\" is wrong. You need "C:\\xampp\\htdocs\\".

Alternative syntax

Alternative syntax

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:\xampp\htdocs\" is wrong. You need "C:\\xampp\\htdocs\\".

Alternative syntax

Second iteration.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 109
  • 132

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.

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:

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.

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:

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.

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:

Active reading [<http://en.wikipedia.org/wiki/NetBeans> <https://en.wiktionary.org/wiki/occurence>]
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 109
  • 132
Loading
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
Loading
added 595 characters in body
Source Link
mario
  • 145.1k
  • 20
  • 240
  • 293
Loading
Unfancy gif to showcase IDE bracket highlighting.
Source Link
mario
  • 145.1k
  • 20
  • 240
  • 293
Loading
Source Link
mario
  • 145.1k
  • 20
  • 240
  • 293
Loading
Post Made Community Wiki by mario