Ffile2fix
Sign in Get started

Fix "syntax error, unexpected token" in PHP

PHP Parse error:  syntax error, unexpected token "}", expecting ";" in /var/www/html/index.php on line 15

PHP's parser met a character or keyword it did not expect at that point, so the file cannot run at all. The cause is usually on the line before the one reported, such as a missing semicolon, an unclosed bracket or quote, or syntax that your PHP version does not support yet.

Also appears as: Parse error: syntax error, unexpected identifier "echo" in /var/www/html/page.php on line 8 Β· Parse error: syntax error, unexpected variable "$name", expecting ")" Β· Parse error: syntax error, unexpected token "match", expecting "(" Β· Parse error: syntax error, unexpected '}' (T_CLOSE_CURLY_BRACE)

Common causes

  • A missing semicolon at the end of the previous statement
  • Unbalanced parentheses, brackets or braces
  • An unescaped quote inside a string
  • Newer syntax on an older PHP version, such as match, enums or readonly on PHP 7.x
  • A reserved word such as match, enum or readonly used as a function or constant name after a PHP upgrade
  • Code pasted from a website with curly quotes or invisible characters

How to fix it

  1. Read the token and expectation. unexpected token "}", expecting ";" means a statement before the } was not ended with a semicolon. The expecting part tells you what PHP needed.
  2. Check the line above. Look at the end of the previous line first. Missing semicolons and unclosed parentheses are reported on the next line where PHP finally notices.
  3. Lint after each fix. Run php -l file.php. Parse errors stop at the first problem, so repeat until it prints No syntax errors detected.
  4. Confirm the PHP version. Run php -v and check the web server's version in phpinfo(). Code using match (8.0), enums (8.1) or readonly classes (8.2) fails with a parse error on older versions; upgrade PHP or use the plugin version made for yours.
  5. Retype pasted characters. Replace curly quotes (β€œ ” β€˜ ’) with straight quotes and delete non-breaking spaces. Pasting from blogs or word processors often introduces them.

Typical fix: missing semicolon

<?php
// Broken: Parse error: unexpected token "}", expecting ";"
if ($ok) {
    echo 'Saved'
}

// Fixed
if ($ok) {
    echo 'Saved';
}

How to stop it happening again

  • Run php -l or a linter in CI and pre-commit hooks
  • Use an editor with PHP syntax checking
  • Set the PHP version in composer.json ("php": ">=8.1") so incompatible code fails early
  • Test on the same PHP version as production

Frequently asked questions

Why is the error on a line that looks fine?

PHP reports where it noticed the problem, which is often one line after the real mistake. Always check the end of the previous statement.

What did T_CLOSE_CURLY_BRACE or T_STRING mean?

Those are token names used in PHP 7 messages. PHP 8 replaced them with friendlier wording such as unexpected token "}" or unexpected identifier.

A plugin update caused this. How do I recover?

Rename the plugin's folder in wp-content/plugins over SFTP to deactivate it. Then check whether the plugin requires a newer PHP version than your server runs.