How to Fix PHP "Parse error: syntax error, unexpected"
A PHP parse error stops the file before it runs — a missing semicolon, an unmatched brace, an unclosed string, or newer syntax on an older PHP. Here is how to find and fix each.
A PHP Parse error: syntax error, unexpected … is a compile-time error: the parser hit a token that is not valid at that position, so the file never runs at all. The reported line is where PHP *noticed* the problem, which is usually at or just after the real mistake — a missing ;, ), }, or quote often reports on the following line.
The 6 common causes and their fixes
1. A missing semicolon
A statement is not terminated, so the parser trips on the next line’s first token.
// ❌ unexpected token "echo" (PHP 8) / unexpected 'echo' (T_ECHO) (PHP 7)
$a = 5
echo $a;
// ✅
$a = 5;
echo $a;
2. Unmatched braces, parentheses or brackets
A missing closer usually yields unexpected end of file; an extra one yields unexpected token "}".
// ❌ the if { is never closed -> unexpected end of file
function f() {
if (true) {
echo "hi";
}
// ✅
function f() {
if (true) {
echo "hi";
}
}
3. A missing $ or a missing operator between tokens
Variables need $, and two value tokens with nothing between them is a syntax error.
$name = "Sam";
// ❌ unexpected variable "$name"
echo Hello $name;
// ✅
echo "Hello " . $name;
4. An unclosed string / missing quote
An unterminated string swallows the rest of the file until the next quote or end of file.
// ❌ missing closing quote -> unexpected end of file
$msg = "hello;
echo $msg;
// ✅
$msg = "hello";
echo $msg;
5. A missing concatenation operator
Adjacent strings and variables need a . between them.
// ❌ unexpected variable "$total"
echo "Total: " $total;
// ✅
echo "Total: " . $total;
6. Disabled short tags, or newer syntax on an older PHP
<? fails when short_open_tag is off; and modern syntax parsed by an older engine errors.
// ❌ short tag when short_open_tag is off
<? echo $x; ?>
// ✅ full tag, or the always-on short echo tag (PHP 5.4+)
<?php echo $x; ?>
<?= $x ?>
// version-specific: arrow fn / typed props need 7.4+, match needs 8.0+
// on an older engine these throw "unexpected token" -> check `php -v`
Quick diagnostic checklist
- Run
php -l file.php(lint, no execution) — it reports the first parse error and its line. - Look at the reported line AND the line(s) immediately above for a missing
;, quote, or closer. - Use an editor with bracket matching to find the unbalanced
{ } ( ) [ ]or the unterminated string. - Check
php -v— is the syntax (arrow functions,match, typed properties) supported by this version? - Replace
<?with<?php(or<?= ?>for echo) in case short tags are disabled.
Frequently asked questions
Why is the PHP parse error on the wrong line?
What does "unexpected end of file" mean in PHP?
Why did the PHP 8 error wording change?
variable "$x", identifier "x" and token "::" replace the old (T_VARIABLE) / (T_STRING) token codes. The causes are unchanged — only the message text differs.Check your PHP syntax without a local server in the XCODX online compiler — it runs PHP in the browser, so you can paste a file, see the exact parse error, fix it, and re-run in seconds.