Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP Parse error: syntax error, unexpected token

ErrorParse error: syntax error, unexpected variable "$b" in /var/www/app/index.php on line 3

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

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.php
  2. 2Match what you see, then jump to its fix

    1. 1unexpected variable "$b" on the line after a statement that has no semicolonA missing semicolon, quote or bracket on the line beforeFix
    2. 2unexpected string content, inside echo "...$arr['key']..."A quoted array key inside a double-quoted stringFix
    3. 3unexpected 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. 4Raw 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

What does the error look like?

  • Parse error: syntax error, unexpected variable "$b" in /var/www/app/index.php on line 3
  • Parse error: syntax error, unexpected end of file
  • Parse error: Unclosed '{' on line 2
  • Parse 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.php

2Fix

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

Confirmed by php.net

Did this fix it?

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

2Fix

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

Confirmed by php.net

Did this fix it?

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

2Fix

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

Confirmed by php.net

Did this fix it?

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_tag

2Fix

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

Confirmed by php.net

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.