Skip to content

PHP Parse Error: syntax error Fix

DodaTech Updated 2026-06-24 4 min read

In this tutorial, you'll learn about PHP Parse Error: syntax error Fix. We cover key concepts, practical examples, and best practices.

The Problem

Your PHP script fails with:

Parse error: syntax error, unexpected end of file in /var/www/html/index.php on line 25

Or:

Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in /var/www/html/index.php on line 20

A parse error means PHP cannot understand your code because of a syntax mistake. The script never executes -- the parser stops immediately with the error. Common causes are missing semicolons, unmatched braces, incorrect string quoting, or closing tags in the wrong place.

Quick Fix

Step 1: Check the line before the error

PHP reports the error at the line where it detected the problem, but the actual mistake is usually on the preceding line:

Parse error: syntax error, unexpected '}', expecting ',' or ';' on line 20

Look at line 19 -- you probably forgot the semicolon.

WRONG -- missing semicolon:

$name = "John"
echo $name;

RIGHT -- add the semicolon:

$name = "John";
echo $name;

Step 2: Match your brackets

WRONG -- missing closing parenthesis:

if ($x > 10 {
    echo "Large";
}

RIGHT -- close all brackets:

if ($x > 10) {
    echo "Large";
}

Use an editor with bracket matching (VS Code, PHPStorm) to spot mismatches. The cursor highlights the matching bracket when you click on one.

Step 3: Check string quotes

WRONG -- mismatched or unclosed quotes:

$message = 'Hello world;
$name = "John';

RIGHT -- use consistent quotes:

$message = 'Hello world';
$name = "John";

For strings containing quotes, escape them or use the opposite quote type:

$text = "It's working";   // single quote inside double quotes is fine
$text = 'It\'s working';  // or escape inside single quotes

Step 4: Check array syntax

WRONG -- using old array syntax with trailing comma in a non-associative array:

$items = array(1, 2, 3,);

This is fine in PHP 7.3+, but older versions may produce a parse error.

WRONG -- missing comma in array:

$items = [1, 2 3];  // missing comma

RIGHT:

$items = [1, 2, 3];

Step 5: Check for stray PHP closing tags

WRONG -- extra whitespace after closing tag:

<?php
echo "Hello";
?>
   <- trailing whitespace or newlines after ?php can cause "headers already sent" errors

For pure PHP files, omit the closing tag entirely:

<?php
echo "Hello";
// No closing tag -- prevents accidental whitespace issues

Step 6: Use php -l to lint your files

php -l index.php
No syntax errors detected in index.php

Lint checks for parse errors without executing the script:

php -l index.php
PHP Parse error:  syntax error, unexpected end of file in index.php on line 25

Lint all files in a project:

find . -name "*.php" -exec php -l {} \;

Use DodaTech's PHP Linter to batch-check syntax across your entire project and highlight the exact line of each parse error.

Prevention

  • Use an editor or IDE with real-time syntax highlighting.
  • Run php -l as part of your pre-commit hook or CI pipeline.
  • Enable short_open_tag = Off to avoid conflicts with XML.
  • Always omit the closing ?> in pure PHP files.
  • Use a code formatter (PHP CS Fixer, PHP_CodeSniffer) to enforce consistent syntax.

Common Mistakes with parse error

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world PHP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does PHP report the error on a different line than where the problem is?

The PHP parser reports where it realized something is wrong, not where the mistake started. For example, a missing semicolon on line 10 is only detected when the parser sees the next statement on line 11. Always look at the line before the error line.

What is T_ECHO, T_STRING, etc. in parse error messages?

These are PHP token names. T_ECHO is the echo keyword, T_STRING is an identifier (function name, class name), T_VARIABLE is a $variable. The error shows what PHP found unexpectedly: unexpected 'echo' (T_ECHO) means PHP did not expect an echo statement at that position.

How do I find all parse errors in a large project?

Use find . -name "*.php" -exec php -l {} \; 2>&1 | grep -v "No syntax errors". This lints every PHP file and shows only files with errors. Redirect to a file for review.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro