PHP parse/syntax errors: how to read and fix errors for beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: why does PHP complain and what to do about it?

\n\n

You write code, you're sure everything is perfect, you hit "Refresh" in the browser — and you see something like:

\n\n
Parse error: syntax error, unexpected '}' in /home/user/index.php on line 15
\n\n

Sound 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\n

In 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\n

What are PHP parse/syntax errors in simple terms?

\n\n

Imagine 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\n

A 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\n

Here 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
\n\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\n

How to read a PHP error message?

\n\n

A typical message looks like this:

\n\n
Parse error: syntax error, unexpected '}' in /var/www/index.php on line 15
\n\n

Let'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
\n\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\n

Top 5 most common PHP parse/syntax errors (with examples)

\n\n

1. Missing semicolon ;

\n

The most popular error among beginners. Every statement in PHP must end with ;.

\n\n

Incorrect:

\n
<?php\necho "Hello, world"\necho "How are you?";\n?>
\n\n

Error:
\nParse error: syntax error, unexpected 'echo' (T_ECHO) in /test.php on line 3

\n\n

Correct:

\n
<?php\necho "Hello, world";\necho "How are you?";\n?>
\n\n

2. Unclosed or incorrect brackets

\n

Every opening bracket { must have a closing bracket }. And vice versa.

\n\n

Incorrect:

\n
<?php\nif (true {\n    echo "Condition met";\n}\n?>
\n\n

Error:
\nParse error: syntax error, unexpected '{' in /test.php on line 2

\n\n

Correct:

\n
<?php\nif (true) {\n    echo "Condition met";\n}\n?>
\n\n

3. Extra closing bracket

\n

Sometimes we put } where it's not needed.

\n\n

Incorrect:

\n
<?php\n$arr = [1, 2, 3];\necho $arr[0];\n}\n?>
\n\n

Error:
\nParse erro

Blogs

Book Recommendations