Introduction: why does PHP complain and what to do about it?
\n\nYou write code, you're sure everything is perfect, you hit "Refresh" in the browser — and you see something like:
\n\nParse error: syntax error, unexpected '}' in /home/user/index.php on line 15\n\nSound familiar? If you've ever encountered PHP parse/syntax errors, you know that feeling of mild panic. It seems like the computer has gone crazy, and you're a terrible programmer. But in reality, it's much simpler. PHP isn't trying to offend you — it's just honestly saying: "I don't understand what you want from me."
\n\nIn this article, we'll break down what a PHP syntax error is, how to read it, find it, and most importantly — fix it. The material is designed for beginners, so no magic — just practice and real-life examples. Let's go!
\n\nWhat are PHP parse/syntax errors in simple terms?
\n\nImagine you're writing a letter to a friend, but you forget to put a period at the end of the sentence. Your friend will probably figure out what you meant. But PHP is not a friend. PHP is a strict teacher who demands perfect order.
\n\nA Parse error occurs when PHP cannot parse your code due to a violation of the language rules. Simply put, you have violated PHP syntax.
\n\nHere are the most common causes:
\n- \n
- Missing semicolon
;\n - Extra or missing bracket
{},(),[]\n - Unclosed quote or string \n
- Incorrect use of keywords \n
- Extra characters in the code \n
The good news: PHP always hints at the line and type of error. Our task is to learn how to read these hints.
\n\nHow to read a PHP error message?
\n\nA typical message looks like this:
\n\nParse error: syntax error, unexpected '}' in /var/www/index.php on line 15\n\nLet's break it down piece by piece:
\n- \n
- Parse error — type of error (syntax) \n
- syntax error, unexpected '}' — what exactly PHP did not expect to see \n
- /var/www/index.php — the file where the error occurred \n
- on line 15 — the line where PHP stumbled \n
Important life hack: PHP often indicates the line where it discovered the problem, but the real cause may be 1-2 lines above. For example, if you forgot a semicolon on line 14, the error may appear on line 15.
\n\nTop 5 most common PHP parse/syntax errors (with examples)
\n\n1. Missing semicolon ;
\nThe most popular error among beginners. Every statement in PHP must end with ;.
Incorrect:
\n<?php\necho "Hello, world"\necho "How are you?";\n?>\n\nError:
\nParse error: syntax error, unexpected 'echo' (T_ECHO) in /test.php on line 3
Correct:
\n<?php\necho "Hello, world";\necho "How are you?";\n?>\n\n2. Unclosed or incorrect brackets
\nEvery opening bracket { must have a closing bracket }. And vice versa.
Incorrect:
\n<?php\nif (true {\n echo "Condition met";\n}\n?>\n\nError:
\nParse error: syntax error, unexpected '{' in /test.php on line 2
Correct:
\n<?php\nif (true) {\n echo "Condition met";\n}\n?>\n\n3. Extra closing bracket
\nSometimes we put } where it's not needed.
Incorrect:
\n<?php\n$arr = [1, 2, 3];\necho $arr[0];\n}\n?>\n\nError:
\nParse erro