PHP syntax: tags, statements, comments and echo

The building blocks of every PHP file, and the mistakes that cause most early errors.

  • Beginner
  • 4 min read
  • Updated 17 Oct 2026

What you will learn

  • Open and close PHP code correctly
  • End statements with semicolons
  • Write single-line and multi-line comments
  • Output text with echo

Every PHP file follows a few simple rules. Learn them now and most "syntax error" messages will make sense straight away.

PHP tags

PHP code lives between an opening <?php tag and an optional closing ?> tag. Anything outside the tags is sent to the output unchanged.

PHP
<p>This line is plain HTML.</p>
<p>This line comes from PHP: <?php echo 'Hello'; ?></p>

For files that contain only PHP, leave out the closing tag:

PHP
<?php

echo 'This file is pure PHP.';

Omitting ?> at the end of pure PHP files prevents a classic bug: stray blank lines after the closing tag get sent to the browser and can break redirects with "headers already sent" errors.

There is also a short form for echoing a value inside HTML: <?= $title ?> is the same as <?php echo $title; ?>.

Statements and semicolons

A statement is one instruction. Each statement ends with a semicolon:

PHP
<?php

echo 'First';
echo 'Second';

Forgetting a semicolon is the most common beginner error:

PHP
<?php

echo 'First'
echo 'Second';
Plain text
PHP Parse error: syntax error, unexpected token "echo", expecting "," or ";" in hello.php on line 4

The error points at line 4, but the missing semicolon is at the end of line 3. PHP only notices something is wrong when it reaches the next statement. When you see a parse error, check the line before the one reported too.

Whitespace and case

  • PHP ignores extra spaces, tabs and blank lines between statements. Use them to make code readable.
  • Variable names are case-sensitive: $name and $Name are different variables.
  • Keywords and function names are not case-sensitive (ECHO works), but always write them in lowercase. Everyone else does.

Comments

Comments are notes for humans. PHP ignores them.

PHP
<?php

// A single-line comment.
# Also a single-line comment, less common.

/*
 * A multi-line comment.
 * Useful for longer explanations.
 */

echo 'Comments do not appear in the output'; // A comment after code.

Good comments explain why code does something, not what it does. // add 1 to $count adds nothing; // Retry once because the API sometimes times out saves the next developer time.

Output with echo and print

echo sends text to the output. It can take several values separated by commas:

PHP
<?php

echo 'Hello', ' ', 'world', "\n";
echo 'Total: ' . 42 . "\n";

The dot . joins (concatenates) strings. You will see much more of it in the strings lesson.

print does almost the same but takes one value. Prefer echo; it is what most code uses.

For checking values while you learn, var_dump() is more useful because it also shows the type:

PHP
<?php

var_dump(42);
var_dump('42');
var_dump(true);
Plain text
int(42)
string(2) "42"
bool(true)

Mixing PHP and HTML

PHP was designed to sit inside HTML. This page lists three items with a loop (you will learn loops later; for now notice how PHP and HTML alternate):

PHP
<ul>
<?php foreach (['Tea', 'Coffee', 'Water'] as $drink): ?>
    <li><?= $drink ?></li>
<?php endforeach; ?>
</ul>

For bigger projects you will keep logic and HTML in separate files, but this mixing is common in templates, including WordPress themes.

Summary

  • Code goes between <?php and ?>; leave out ?> in pure PHP files.
  • End every statement with a semicolon. If a parse error seems wrong, check the previous line.
  • Variable names are case-sensitive.
  • Use // and /* */ for comments that explain why.
  • echo outputs text; var_dump() shows values with their types.

Further reading