PHP Parse Error: syntax error Fix
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 -las part of your pre-commit hook or CI pipeline. - Enable
short_open_tag = Offto 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
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro