Troubleshooting

Fix PHP Parse error: syntax error, unexpected

Understand and fix PHP parse errors such as unexpected end of file, unexpected token, unexpected identifier and T_STRING, including errors caused by an old PHP version.

BeginnercPanel hosting · Linux VPS3 min read

A PHP parse error means PHP could not read the file at all, so none of it runs. The site usually shows a white page or a 500 error. The message names a file and a line number, which is where PHP noticed the problem. The actual mistake is often a line or two earlier.

Before you start

  • Find the full error in the PHP error log. On cPanel, see find PHP errors in cPanel.
  • Keep a copy of the file before you edit it.

Read the error message

PHP Parse error: syntax error, unexpected token "}", expecting ";" in /home/user/public_html/inc/functions.php on line 42

Go to line 42, then look at the line above it. Here, a statement on line 41 is missing its semicolon.

Lint the file from the command line

Over SSH, check a file without running it:

php -l inc/functions.php

To check a whole project:

find . -name "*.php" -exec php -l {} \; | grep -v "No syntax errors"

Common variants and causes

MessageUsual cause
unexpected end of fileA missing closing }, or an unclosed string or comment
unexpected token "}" / ")"A missing ; or an extra bracket on the line before
unexpected identifier / T_STRINGUnescaped quotes inside a string, or a missing . between strings
unexpected variableA missing operator or semicolon before a $variable
unexpected token "?" or "=>" / "fn"Newer syntax on an older PHP version

When the code is fine but PHP is too old

Plugins and themes written for PHP 8 use features like ?->, match, named arguments and typed properties. On PHP 7.x these produce parse errors even though the code is correct. Check which version the site uses and update it:

Plan the change with the PHP version upgrade checklist.

Errors after editing in the browser

If the error appeared right after you edited a WordPress theme or plugin file, upload the original file over FTP or the File Manager, or rename the plugin folder to disable it. Common slips are a deleted ?>, curly quotes pasted from a word processor, and code pasted after a closing tag.

FAQ

Why does the error point to the last line of the file?

"Unexpected end of file" means PHP reached the end while still waiting for a closing bracket or quote. Use an editor with bracket matching to find the unclosed block.

Can a parse error be caused by a file upload?

Yes. A file that uploaded only partly, or in ASCII FTP mode, can be cut off. Upload it again in binary mode.

Why don't I see the error, only a white page?

Display of errors is off in production. The message is still written to the error log; read it there rather than turning on display_errors on a live site.

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.