"Parse error: syntax error, unexpected end of file" in PHP — What It Means and How to Fix It

What the PHP error "Parse error: syntax error, unexpected end of file" means and how to fix it.

Published November 1, 2026

The parser reached the end of the file while still waiting for something, almost always a missing closing brace, parenthesis or quote earlier in the file.

What it means and why it happens

Because the error appears at the last line, the real mistake is above it. An editor with bracket matching finds it quickly.

Read the full message: it names the file and line where the problem was detected, which is the best place to start.

What you see

run the code
Parse error: syntax error, unexpected end of file in script.php on line 40

Practical examples

Code that triggers it

<?php
if ($ok) {
    echo 'yes';
// missing closing brace

The fix

<?php
if ($ok) {
    echo 'yes';
}

Find it fast

php -l script.php
# use editor bracket matching or format the file with php-cs-fixer

Common mistakes

  • Looking at the last line only.
  • Short open tags (<?) disabled on the server.
  • Heredoc end markers indented incorrectly.

Key takeaways

  • Unexpected end of file: The parser reached the end of the file while still waiting for something, almost always a missing closing brace, parenthesis or quote earlier in the file.
  • Read the message and the line number first.
  • Fix the cause, don't just silence the error.

FAQ

Why is the line number the last line?

Because PHP only discovers the problem when the file ends.

How do I find which line caused Unexpected end of file?

The error message and stack trace show the file and line; start there and work backwards.

More PHP articles