PHP Parse error: syntax error, unexpected $end — Causes and Fix
Problem: site pages show Parse error: syntax error, unexpected $end.
In short: PHP reached the end of the file while code was still “open”. The usual culprits are short <? tags with short_open_tag turned off, or an unclosed curly brace. In newer PHP versions the same error reads syntax error, unexpected end of file.
Cause 1: short <? tags with short_open_tag = Off
I got this error after moving perfectly working scripts to another server. The code used short <? tags instead of <?php, and support for them was turned off on the new server. PHP didn’t treat that code as code, so in the end the closing part was missing.
The quick fix is to enable short tags in php.ini:
short_open_tag = On
Restart Apache or PHP-FPM after the change, or the setting won’t apply.
The proper fix is to replace <? with <?php in your scripts: then the code works on any server regardless of settings. The <?= short echo tag always works in PHP 5.4 and later, regardless of short_open_tag.
Cause 2: an unclosed brace or quote
If short tags aren’t the issue, look at the syntax. As readers pointed out in the comments:
- a curly brace
}isn’t closed in a loop, condition or function; - a quote or heredoc isn’t closed (
<<<EOTwithout the closingEOT;); - a template opens
if ():orforeach ():and theendif;/endforeach;got lost.
How to find the error quickly
Check the file with PHP’s built-in syntax check:
php -l index.php
The line number in the message usually points to the end of the file — so look above it for something opened and never closed. An editor that highlights matching braces (Notepad++, VS Code) finds it in a minute.
