PHP Parse error: syntax error, unexpected token
Parse error: syntax error, unexpected variable "$b" in /var/www/app/index.php on line 3Short answer
A parse error means PHP could not read the file, so nothing ran. The line in the message is where PHP noticed the problem, and the mistake is usually on the line before: a missing semicolon, quote or bracket. Run php -l on the file, then check the line above.
Fix it fast
1Find out which cause you have
Syntax-check the file without running it. A broken file reports the parse error; a clean one prints No syntax errors detected.
php -l index.php2Match what you see, then jump to its fix
- 1
unexpected variable "$b" on the line after a statement that has no semicolonA missing semicolon, quote or bracket on the line beforeFix - 2
unexpected string content, inside echo "...$arr['key']..."A quoted array key inside a double-quoted stringFix - 3
unexpected identifier "FOO" or Invalid body indentation level on a file that worked on PHP 7.2 or olderA heredoc whose closing label is wrong or also appears in the textFix - 4
Raw PHP source printed in the page, or a parse error on the first line of an old fileA file uses <? short tags and the server has short_open_tag offFix
- 1
What does the error look like?
Parse error: syntax error, unexpected variable "$b" in /var/www/app/index.php on line 3Parse error: syntax error, unexpected end of fileParse error: Unclosed '{' on line 2Parse error: syntax error, unexpected identifier "FOO"Parse error: syntax error, unexpected token
Cause 1 of 4
A missing semicolon, quote or bracket on the line before
1Check
PHP reads on until a token no longer fits. The reported token is the first one that breaks the grammar, so the cause is usually just above it. For unexpected end of file or Unclosed, a { or ( was never closed.
php -l index.php
sed -n '1,6p' index.php2Fix
End the statement with a semicolon, or close the quote or bracket. Count opening and closing braces if the error is at end of file.
<?php
$a = "x";
$b = 1;3Verify
The syntax check passes.
php -l index.phpConfirmed by php.net
Cause 2 of 4
A quoted array key inside a double-quoted string
1Check
In a double-quoted string the simple form accepts an unquoted key only. A quoted key there is a parse error.
grep -n -E '"[^"]*\$[a-z_]+\[' index.php2Fix
Use curly braces around the whole expression, or concatenate.
<?php
echo "Hello {$user['name']}";
echo 'Hello ' . $user['name'];3Verify
The syntax check passes.
php -l index.phpConfirmed by php.net
Cause 3 of 4
A heredoc whose closing label is wrong or also appears in the text
1Check
Since PHP 7.3 the closing label may be indented, so a body line that starts with the label (even inside a longer word) ends the string early. Indenting the closing label deeper than the body is a ParseError too.
grep -n -E '<<<|^[[:space:]]*(EOT|EOD|FOO);?' index.php2Fix
Choose a closing label that does not occur in the text, and indent the closing label no more than the body lines.
<?php
$sql = <<<SQL
SELECT id
FROM users
SQL;3Verify
The syntax check passes.
php -l index.phpConfirmed by php.net
Cause 4 of 4
A file uses <? short tags and the server has short_open_tag off
1Check
The short open tag <? depends on the short_open_tag setting, which can be off. <?php and <?= always work.
grep -n "<?[^p=x]" index.php | head
php -i | grep short_open_tag2Fix
Replace <? with <?php (and make sure a whitespace character follows it).
<?php echo 'portable'; ?>
<p><?= $title ?></p>3Verify
The syntax check passes.
php -l index.phpConfirmed by php.net
How do I stop it coming back?
- Run php -l on every changed file in a pre-commit hook or CI.
- Use an editor with bracket matching and a PHP language server, which flags parse errors as you type.
- Use <?php and <?= only, never bare <?.
- Pick unique heredoc labels like SQL or HTML_EOT, and keep the closing label aligned.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
PHP shows a blank page and no error message
No Warning or Fatal error text, even though the script is broken
A blank page means PHP hit an error but was told not to print it.
4 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix