Skip to content

PHP Tutorial PHP Syntax and Output

Common PHP Syntax Mistakes

Lesson 20 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

Every PHP developer sees syntax errors every day. The skill is not avoiding them. It is reading them quickly. When PHP cannot understand your code at all, it shows a parse error and stops before running anything.

1. Missing semicolon

index.php
PHP
<?php
$a = 5
echo $a;
Output
OUTPUT
Parse error: syntax error, unexpected token "echo" in /path/to/index.php on line 3

The fix is $a = 5;. The error is on line 3, the real problem is on line 2.

2. Missing dollar sign

index.php
PHP
<?php
name = "Ravi";
Output
OUTPUT
Parse error: syntax error, unexpected token "=" in /path/to/index.php on line 2

Every variable starts with $.

3. Unclosed quote

index.php
PHP
<?php
echo "Hello;
Output
OUTPUT
Parse error: syntax error, unexpected end of file, expecting variable or "${" or "{$" in /path/to/index.php on line 2

A string that never ends swallows the rest of the file. PHP only notices at the end of the file, and the exact wording of the message can differ between PHP versions.

4. Missing closing bracket

index.php
PHP
<?php
if (true) {
    echo "Yes";
Output
OUTPUT
Parse error: Unclosed '{' on line 2 in /path/to/index.php on line 3

PHP names the bracket it is missing. Count your { and }.

5. Mixing quotes

index.php
PHP
<?php
echo 'It's raining';

The apostrophe in It's ends the string early. Use double quotes ("It's raining") or escape it ('It\'s raining').

How to read a PHP error

Every message has the same parts:

  1. The type: Parse error, Warning, Fatal error.
  2. What went wrong: the sentence after the colon.
  3. The file and the line number.

Start at the reported line, then look at the lines just above it.

lightbulb
Use an editor with syntax highlighting
A good editor colours strings and underlines unclosed brackets while you type. Many syntax errors never reach the browser.

Exercise: fix three bugs

This code has three syntax mistakes. Find them all.

index.php
PHP
<?php
$city = "Delhi"
echo 'Welcome to $city;
Show the answer

There is a missing semicolon after "Delhi", an unclosed single quote, and single quotes do not print the variable’s value. A fixed version:

index.php
PHP
<?php
$city = "Delhi";
echo "Welcome to $city";
Output
OUTPUT
Welcome to Delhi