PHP Syntax
Learn the fundamental syntax rules of PHP, including tags, statements, case sensitivity, and how code blocks are structured.
Introduction
Every language has its own set of ground rules for how code must be written before the interpreter can make sense of it. PHP's syntax rules are refreshingly small in number, but they matter — get them wrong and PHP will refuse to run your script at all.
In this lesson, you will learn the handful of core syntax rules that apply to every PHP script you will ever write: how to open and close PHP mode, how statements end, whether names are case-sensitive, how whitespace is treated, and how code blocks are grouped with curly braces.
- How to switch between HTML and PHP using <?php ?> tags.
- The short echo tag <?= ?> for quickly printing values.
- Why every PHP statement must end with a semicolon.
- Which parts of PHP are case-sensitive and which are not.
- How whitespace and curly braces shape your code.
PHP Tags
PHP code must be wrapped in special tags so the server knows where PHP begins and ends inside an otherwise ordinary HTML file. The standard opening tag is <?php, and the closing tag is ?>.
<!DOCTYPE html>
<html>
<body>
<p>This is regular HTML.</p>
<?php
echo "This line was printed by PHP.";
?>
<p>Back to regular HTML again.</p>
</body>
</html>This is regular HTML.
This line was printed by PHP.
Back to regular HTML again.PHP also supports a short echo tag, <?= ?>, which is shorthand for <?php echo ?>. It is handy when you just want to drop a single value into HTML without writing a full echo statement.
<?php $name = "Priya"; ?>
<h1>Welcome, <?= $name ?>!</h1>
<!-- The line above is identical to: -->
<h1>Welcome, <?php echo $name; ?>!</h1>Welcome, Priya!
Welcome, Priya!If a file contains only PHP code and nothing follows it, it is common practice to omit the closing ?> tag entirely. This avoids accidental whitespace or blank lines being sent as output after your script ends.
Statements & Semicolons
A PHP script is a series of statements — individual instructions telling the interpreter what to do. Every statement must end with a semicolon (;), which tells PHP "this instruction is complete."
<?php
$price = 250;
$quantity = 3;
$total = $price * $quantity;
echo "Total: $total";
?>Total: 750Forgetting a semicolon is one of the most common PHP errors, especially for beginners. It usually produces a "syntax error, unexpected token" message pointing at the line after the one you actually forgot to end.
Case Sensitivity
PHP is inconsistent — deliberately so — about case sensitivity, and knowing which parts care about it will save you a lot of confusion.
Case-Sensitive
- Variables: $name and $Name are two different variables
- Class names (in modern coding standards, treat as sensitive)
- Array keys and string content
Case-Insensitive
- Function names: strlen(), STRLEN(), and StrLen() all work
- Keywords: if, IF, and If all work
- Class and function declarations themselves
<?php
$color = "blue";
$Color = "red";
echo $color; // blue
echo "\n";
echo $Color; // red - a completely different variable
// Function names ignore case:
echo STRTOUPPER("hello"); // works, though not conventional
?>blue
red
HELLOEven though function names are case-insensitive, always call them using their documented lowercase form (strtoupper, not STRTOUPPER). Consistent casing makes code far easier to read and maintain.
Whitespace
PHP ignores extra whitespace — spaces, tabs, and blank lines — between statements. This means indentation is purely for human readability; it has no effect on how the code executes, unlike languages such as Python.
<?php
$a=5;$b=10;$c=$a+$b;echo $c;
?>
<?php
$a = 5;
$b = 10;
$c = $a + $b;
echo $c;
?>15
15PHP only ignores whitespace between statements. Spaces, tabs, and newlines typed inside a quoted string are preserved exactly as written and become part of the string's value.
Code Blocks with Curly Braces
Because whitespace and indentation carry no meaning in PHP, the language needs another way to know which statements belong together inside a condition, loop, or function. That job belongs to curly braces { }.
<?php
$age = 20;
if ($age >= 18) {
echo "You are an adult.";
echo " You can vote.";
} else {
echo "You are a minor.";
}
?>You are an adult. You can vote.Everything between a matching pair of { and } is treated as one block. Indenting the contents of that block is a strong convention for readability, but PHP itself only cares about the braces.
Because indentation is not enforced, misaligned code can still run perfectly fine, but it becomes confusing and error-prone for humans. Always indent consistently even though PHP does not require it.
Common Mistakes
- Forgetting the semicolon at the end of a statement.
- Mixing up $name and $Name, expecting them to be the same variable.
- Assuming indentation alone groups statements together — it does not, only curly braces do.
- Leaving a closing ?> tag followed by a stray blank line or space in a file that only contains PHP, causing unexpected output ("headers already sent" errors).
Best Practices
- End every statement with a semicolon, even the last one in a block.
- Always call built-in functions in lowercase for consistency.
- Indent code inside curly braces even though PHP does not require it — your future self will thank you.
- Omit the closing ?> tag in files that contain only PHP code.
Frequently Asked Questions
Do I always need the closing ?> tag?
No. It is optional and commonly omitted at the end of pure-PHP files to prevent accidental output from trailing whitespace after the tag.
What is the difference between <?php echo $name; ?> and <?= $name ?>?
They do exactly the same thing. <?= ?> is shorthand for <?php echo ?>, useful for quickly embedding values inside HTML.
Are PHP keywords like if and while case-sensitive?
No. Keywords and function names are case-insensitive, though writing them in lowercase is the universal convention.
Can I remove all the whitespace from my code to make it run faster?
No meaningful performance difference exists. Whitespace is only for readability; removing it just makes your code harder to maintain.
Key Takeaways
- PHP code lives inside <?php ?> tags, with <?= ?> as a shorthand for echoing a value.
- Every statement must end with a semicolon.
- Variables are case-sensitive; function names and keywords are not.
- Whitespace between statements is ignored by PHP but preserved inside strings.
- Curly braces { }, not indentation, define which statements belong to a code block.
Summary
PHP's syntax rules are small but strict: statements end in semicolons, code lives inside <?php ?> tags, variables care about case while functions and keywords do not, and curly braces — not indentation — group statements into blocks.
With these foundational rules in place, you are ready to look at comments — a simple but essential tool for documenting your code as you write it.
- You can correctly open and close PHP mode.
- You know why semicolons are required.
- You understand PHP's mixed approach to case sensitivity.
- You know that curly braces, not indentation, define code blocks.