Syntax Overview
PHP is a C-style, dynamically typed scripting language that was built specifically to sit inside HTML and generate web pages on the server. If you have ever written JavaScript, Java, or C, most of PHP's punctuation will feel familiar immediately: curly braces mark blocks, parentheses wrap conditions, and every statement ends with a semicolon. What makes PHP's syntax distinct is that a file doesn't have to be "all PHP" — it switches between plain HTML output and executable PHP using special tags, and the interpreter only pays attention to what falls between them.
Entering and leaving "PHP mode"
A PHP file is really an HTML (or plain text) file with escape hatches into code. The opening tag <?php tells the interpreter "start executing," and the closing tag ?> tells it "go back to outputting raw text verbatim." Anything outside those tags is sent to the browser exactly as written, with no processing at all.
Mixing HTML and PHP in one file
<!DOCTYPE html>
<html>
<body>
<h1>Today's specials</h1>
<?php
$specialCount = 3;
echo "<p>We have $specialCount specials today.</p>";
?>
<p>Thanks for visiting!</p>
</body>
</html>Everything inside <?php ... ?> is a normal PHP statement list; everything outside it is static markup. This is why you'll sometimes see files that are almost entirely HTML with a few PHP tags sprinkled in, and others (like class files) that open with <?php on line one and never close the tag at all — more on that in the closing-tag guidance below.
Anatomy of a small PHP script
Let's break down a self-contained script line by line to see the core building blocks in context: variables, a function definition, a conditional, and output.
anatomy.php
<?php
// 1. A variable — always starts with $, no declaration keyword needed
$temperatureCelsius = 28;
// 2. A function definition — braces delimit the body, no semicolon after }
function describeWeather(float $celsius): string
{
// 3. A conditional statement block
if ($celsius >= 30) {
return 'hot';
} elseif ($celsius >= 20) {
return 'warm';
}
return 'cool';
}
// 4. A function call whose result is stored in a variable
$description = describeWeather($temperatureCelsius);
// 5. Output — echo sends text to the response
echo "It's a $description day ($temperatureCelsius°C).";
It's a warm day (28°C).
Statements, blocks, and the semicolon rule
Statement vs. block
$count = 0; // a statement — ends in ;
while ($count < 3) { // a block — no ; after the opening or closing brace
echo $count;
$count++; // each line inside the block is its own statement
}Case sensitivity, briefly
PHP's rules here trip up a lot of newcomers, so it's worth stating plainly up front: variables are case-sensitive ($total and $Total are two different variables), but function names, class names, and language keywords are not (echo, ECHO, and Echo all work identically). This topic has its own dedicated page with worked examples — treat this as the headline version.
Whitespace doesn't matter, structure does
PHP ignores extra spaces, tabs, and blank lines between tokens — you
could write an entire script on one line and it would still run.
What does matter is the structural punctuation: semicolons,
matching braces and parentheses, and matching quotes around strings.
A missing closing brace or a stray extra } is one of the most
common sources of a fatal parse error, especially in longer files.
<?phpopens PHP mode;?>returns to raw HTML output.Every statement ends in
;— blocks ({ }) do not.Variables are prefixed with
$and are case-sensitive.Function, class, and keyword names are case-insensitive.
Comments, indentation, and blank lines are purely for humans — the interpreter ignores them.