[Solution] PHP Parse Error — syntax error, unexpected token Fix

A PHP parse error (syntax error, unexpected token) occurs when the PHP parser encounters code that violates the language grammar. PHP cannot execute any code in a file that contains a parse error — it halts immediately and displays the file name, line number, and the problematic token.

Common Causes and Fixes

1. Missing Semicolon

The most frequent syntax error — a missing ; at the end of a statement.

// WRONG — missing semicolon
<?php
$greeting = "Hello"
echo $greeting
?>

Fix: Add the semicolon.

// CORRECT
<?php
$greeting = "Hello";
echo $greeting;
?>

2. Unclosed String

A string without a closing quote breaks the parser.

// WRONG — unclosed string
<?php
echo "Hello, world!;
?>

Fix: Close all strings properly.

// CORRECT
<?php
echo "Hello, world!";
?>

3. Unclosed Bracket or Parenthesis

Every (, [, or { must have a matching closing bracket.

// WRONG — unclosed parenthesis
<?php
function greet($name) {
    echo "Hello " . $name;
// missing closing )

Fix: Close all brackets.

// CORRECT
<?php
function greet($name) {
    echo "Hello " . $name;
}
?>

4. Wrong Array Syntax

Using () instead of [] for array access (PHP 5.4+).

// WRONG — old-style array access
<?php
$arr = array(1, 2, 3);
echo $arr(0); // unexpected '('
?>

Fix: Use square brackets.

// CORRECT
<?php
$arr = [1, 2, 3];
echo $arr[0]; // 1
?>

5. Mixing PHP and HTML Incorrectly

Forgetting to close PHP tags or opening a new <?php block inside one.

// WRONG — unclosed PHP block
<?php
if ($condition) {
?>
    <p>Hello</p>
<?php // missing closing }

Fix: Ensure all control structures have matching braces before and after HTML.

// CORRECT
<?php
if ($condition) {
?>
    <p>Hello</p>
<?php
}
?>

6. Using Single Quotes Inside Single-Quoted Strings

// WRONG — unescaped single quote
<?php
echo 'It's broken'; // unexpected 's'
?>

Fix: Escape the quote or use double quotes.

// CORRECT — escape the quote
<?php
echo 'It\'s fixed';
echo "It's fixed"; // also valid
?>

How to Find Parse Errors

Use php -l (lint) to check syntax without executing the script:

php -l index.php

Example output for a broken file:

Parse error: syntax error, unexpected token "," in /path/to/index.php on line 5
Errors parsing /path/to/index.php

No errors on a valid file:

No syntax errors detected in /path/to/index.php

Useful Linting Commands

# Check a single file
php -l myfile.php

# Check all PHP files in a directory
find . -name "*.php" -exec php -l {} \;

# Check syntax with all errors reported (PHP 8.0+)
php -d display_errors=1 -l myfile.php

Summary

ErrorFix
unexpected ';'Check for missing semicolon on previous line
unexpected T_VARIABLEMissing semicolon or = operator
unexpected ')'Missing opening parenthesis or argument
unexpected ']'Array syntax mismatch
unexpected EOFMissing closing } or ?>
unexpected 'string'Unclosed string or wrong quote nesting

Best Practices

  • Enable display_errors and error_reporting = E_ALL in your php.ini during development.
  • Use php -l as a pre-commit hook to catch syntax errors before they reach production.
  • Configure your editor to highlight mismatched brackets in real time.
  • Use PHP_CodeSniffer or PHPStan for deeper static analysis beyond syntax.