3. PHP Syntax
What you will learn
- How to start and end PHP code
- Statements and semicolons
- What is and is not case sensitive
- How to mix PHP with HTML
PHP tags
PHP code lives between an opening tag <?php and a closing tag ?>. Outside the tags, the server treats everything as plain text and sends it unchanged.
PHP code
<?php
echo "This is PHP output.";
?>
Output
This is PHP output.
Tip: in a file that contains only PHP, leave out the closing ?>. It is allowed, and it prevents stray spaces after the tag from being sent to the browser by accident.
Always write the full <?php. The short form <? depends on server settings and should not be used.
Statements and semicolons
A PHP program is a list of statements. Every statement must end with a semicolon. Forgetting one is the most common beginner error.
PHP code
<?php
echo "First line. ";
echo "Second line.";
Output
First line. Second line.
The two sentences appear on one line because echo does not add a line break. You will learn how to add one in the Echo and Print lesson.
Whitespace
PHP ignores extra spaces and line breaks between statements. Use them to keep code tidy. One statement per line is the common style.
Mixing PHP with HTML
You can open and close PHP as often as you like in one file.
PHP code: mixed.php
<h1>Welcome</h1>
<?php
$year = 2026;
?>
<p>The year is <?php echo $year; ?>.</p>
What the browser receives
<h1>Welcome</h1> <p>The year is 2026.</p>
The blank lines come from the line breaks around the PHP block. Browsers ignore them, so the page looks the same.
The short form <?= $year ?> is equivalent to <?php echo $year; ?>. It is always available in modern PHP and is handy inside HTML.
Case sensitivity
| Item | Case sensitive? |
|---|---|
Variable names ($name and $Name) |
Yes, they are different variables |
| Constant names | Yes |
Keywords such as echo, if, while |
No |
| Function and class names | No |
Even where PHP allows it, write keywords and function names in lowercase, in the same case as the documentation, so code stays consistent.
PHP code
<?php
$color = "blue";
ECHO "The color is " . $color . ". ";
echo "Undefined variable: " . ($Color ?? "not set");
Output
The color is blue. Undefined variable: not set
ECHO works, but $Color is a different variable from $color, so it has no value. (The ?? operator gives a fallback when something is not set. You will meet it later.)
Blocks and braces
A group of statements wrapped in curly braces { } is a block. You will use blocks with conditions, loops, and functions.
PHP code
<?php
$logged_in = true;
if ($logged_in) {
echo "Welcome back!";
}
Output
Welcome back!
Notice that the closing brace needs no semicolon after it.
Common mistakes
- Forgetting the semicolon, which causes a “syntax error” message.
- Forgetting the opening
<?php, so the server prints the code as text. - Using the wrong capital letters in a variable name.
- Using curly or “smart” quotes copied from a word processor instead of plain straight quotes.
- Leaving spaces or blank lines after a closing
?>in a pure-PHP file. - Forgetting the
.phpfile extension.
Reading an error message
If you forget a semicolon, PHP stops and shows a message that names the file and the line number. The reported line is where PHP noticed the problem, so also check the line above it.
Exercise
This file does not work. Find and fix the two mistakes.
<?php
echo "Hello"
echo "World";
Show solution
The first statement has no semicolon, so PHP cannot tell where it ends. Fixed version:
<?php
echo "Hello";
echo "World";
The second “mistake” is that the output HelloWorld has no space. Add one inside the first string ("Hello ") if you want a gap.
Knowledge check
1. What ends a PHP statement?
A semicolon.
2. Are $name and $Name the same variable?
No. Variable names are case sensitive.
3. Why omit the closing ?> in a file that contains only PHP?
It avoids accidental extra spaces or line breaks being sent after the tag.
Summary
- PHP code goes between
<?phpand?>, and every statement ends with;. - Variable names are case sensitive. Keywords and function names are not.
- You can mix PHP and HTML freely in one file.