PHPSyntax Overview

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

PHP
<!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
<?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
Every individual instruction in PHP — a variable assignment, a function call, a `return` — is a **statement**, and every statement must end with a semicolon (`;`). This is not optional whitespace styling like it is in JavaScript with automatic semicolon insertion; PHP has no such fallback, and a missing semicolon is a parse error. Multiple statements group into a **block** using curly braces ``, which is how `if`, `for`, `while`, `function`, and `class` bodies are delimited. Blocks themselves are not statements and never take a trailing semicolon after the closing brace.

Statement vs. block

PHP
$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.

  • &lt;?php opens PHP mode; ?&gt; 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.

PHP is a preprocessor, not just a language
The name "PHP" originally stood for "Personal Home Page," and the engine's job is still literally to walk through a file, output the static text as-is, and execute anything between PHP tags in order. Thinking of it as an HTML preprocessor — rather than a language that happens to output HTML — makes the tag-switching syntax click.
Tip
For any file that contains **only** PHP code (a class, a config file, a function library — no trailing HTML), skip the closing `?>` tag entirely. It isn't required, and omitting it prevents an entire class of bugs caused by accidental whitespace or newlines after the tag leaking into the HTTP response before your headers are sent. PSR-12 codifies this as the standard practice.