Ffile2fix
Sign in Get started

Fix "syntax error, unexpected end of file" in PHP

PHP Parse error:  syntax error, unexpected end of file in /var/www/html/functions.php on line 214

PHP read to the end of the file and something was still open: a curly brace, a parenthesis, a quoted string, a heredoc, or an alternative-syntax block like if: ... endif;. The line number points to the end of the file, so the real mistake is somewhere earlier. PHP 8 often reports this as "Unclosed '{' on line N", which tells you where the open block started.

Also appears as: Parse error: Unclosed '{' on line 12 in /var/www/html/index.php on line 80 · Parse error: syntax error, unexpected end of file, expecting "," or ";" · Parse error: syntax error, unexpected end of file, expecting variable or heredoc end or "${" or "{$" · Parse error: syntax error, unexpected $end

Common causes

  • A missing closing curly brace } for a function, class, if or loop
  • An unterminated string, often an apostrophe inside a single-quoted string
  • A heredoc or nowdoc whose closing identifier is missing or misspelled
  • Short open tags <? used when short_open_tag is Off, so a block is hidden from PHP
  • An if: / foreach: block missing its endif; or endforeach;
  • A file truncated during upload or FTP transfer

How to fix it

  1. Lint the file. Run php -l path/to/file.php. On PHP 8 the message often includes Unclosed '{' on line N, which is the block that never closed.
  2. Match the braces. Open the file in an editor with bracket matching and code folding. Collapse each function and class; the one that swallows the rest of the file is missing its closing brace.
  3. Check strings and heredocs. Look for strings where syntax highlighting runs on for many lines. For heredoc, the closing identifier (for example EOT;) must match exactly, and before PHP 7.3 it had to start at column 0.
  4. Replace short open tags. Change <? to <?php everywhere (keep <?= which is always enabled). With short_open_tag = Off, code after <? is treated as HTML, leaving braces unbalanced.
  5. Re-upload or restore the file. If you did not edit the file recently, compare its size with the original package or your Git copy. A truncated upload is fixed by uploading the file again in binary mode or restoring it from backup.

Lint every PHP file in a project

# single file
php -l functions.php

# whole project, show only failures
find . -name '*.php' -not -path './vendor/*' -print0 \
  | xargs -0 -n1 php -l | grep -v '^No syntax errors'

How to stop it happening again

  • Run php -l in a Git pre-commit hook or CI step
  • Use an editor or IDE that highlights unmatched brackets
  • Edit live files through Git or SFTP, not a browser-based editor that can save partial files
  • Avoid short open tags in shared code

Frequently asked questions

Why is the line number the last line of the file?

PHP only discovers the problem when it runs out of code. The actual mistake, such as a missing brace, is earlier in the file.

What does 'unexpected $end' mean?

It is the PHP 5 and early PHP 7 wording of the same error. PHP 8 says 'unexpected end of file' or 'Unclosed' with the opening line.

My site broke after editing functions.php in WordPress. What now?

Connect by SFTP or the hosting file manager, open wp-content/themes/your-theme/functions.php and fix or revert the edit. If you cannot find it, restore the file from the theme package or a backup.