PHP Parse error: syntax error, unexpected ...
PHP Parse error: syntax error, unexpected ...
DodaTech
2 min read
The “syntax error, unexpected” parse error means PHP’s parser encountered a token it did not expect at that position in your code. This stops parsing immediately and the script never runs.
What It Means
PHP reads your source code token by token. When the sequence of tokens violates the language grammar, the parser throws a syntax error. The error message tells you what token was unexpected (e.g., }, ;, T_STRING) and the line number where it occurred.
Why It Happens
- Missing semicolon — the most common cause; one line lacks
;and the next line’s token becomes unexpected. - Unmatched braces or parentheses — too many or too few
{},(), or[]. - Wrong quote type — using
"inside a"string without escaping. - Missing
$on variables — PHP expects a$before variable names. - T_PAAMAYIM_NEKUDOTAYIM — the double-colon
::used incorrectly, often when referencing a class constant or static method on a non-class. - Trailing commas in function calls (allowed in PHP 8.0+ for arguments, but not for parameter lists in older versions).
How to Fix It
1. Add the missing semicolon
$name = "John"
echo $name; // ❌ Parse error on this line
// Fix:
$name = "John";
echo $name; // ✅
2. Match all braces and parentheses
function greet($name {
echo "Hello, $name";
} // ❌ Missing closing )
// Fix:
function greet($name) {
echo "Hello, $name";
} // ✅
3. Escape quotes properly
$text = "She said "hello""; // ❌
$text = "She said \"hello\""; // ✅
$text = 'She said "hello"'; // ✅ also works
4. Never use :: on non-classes
$result = $myVariable::CONSTANT; // ❌ T_PAAMAYIM_NEKUDOTAYIM
$result = MyClass::CONSTANT; // ✅
5. Check for misplaced keywords
echo "Hello"
if ($x > 5) { ... } // ❌ Missing ; after echo
echo "Hello";
if ($x > 5) { ... } // ✅
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro