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
- 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.
- 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.
- 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.
- 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.
- 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