Syntax and Comments
PHP's basic rules are short: code goes inside PHP tags, statements end with semicolons, blocks use braces, and comments are ignored by the interpreter. This lesson covers those rules, the surprising mix of case-sensitive and case-insensitive names, and the coding style most PHP projects follow.
Statements and blocks
A statement is one instruction ending in ;. Curly braces group statements into a block — the body of an if, a loop or a function:
<?php $hour = 14; if ($hour < 12) { echo "Good morning\n"; echo "Coffee time.\n"; } else { echo "Good afternoon\n"; }
Good afternoon
Spaces, tabs and line breaks between tokens don't matter to PHP, so indentation is purely for humans — but it matters a lot to them. Indent blocks by four spaces.
Case sensitivity
PHP is inconsistent here, for historical reasons:
- Variables are case-sensitive:
$totaland$Totalare different variables. - Keywords, functions and class names are not:
ECHO,StrLen()andstrlen()all work.
<?php $color = "red"; $Color = "blue"; echo $color, " ", $Color, "\n"; ECHO STRTOUPPER("works anyway"), "\n";
red blue WORKS ANYWAY
Just because you can write STRTOUPPER doesn't mean you should: always use the exact names from the documentation, in lower case for keywords and built-in functions.
Comments
PHP has three comment styles:
<?php // A single-line comment # Also a single-line comment (less common) /* A multi-line comment, useful for longer explanations. */ $seats = 120; // a comment can follow code $booked = 87; echo $seats - $booked, "\n"; // free seats
33
Write comments that explain why the code does something, not what it does — the code already shows that. Commenting out a line is also a quick way to switch it off while testing.
Doc comments
A comment starting with /** placed before a function or class is a docblock. Editors and documentation tools read its @param and @return tags:
<?php /** * Converts a temperature from Fahrenheit to Celsius. * * @param float $fahrenheit Temperature in degrees Fahrenheit. * @return float Temperature in degrees Celsius. */ function celsius(float $fahrenheit): float { return ($fahrenheit - 32) * 5 / 9; } echo celsius(212), "\n";
100
With modern type declarations (lesson 29) the types are already in the code, so docblocks focus on explaining meaning, units and edge cases.
Attributes
Since PHP 8, #[...] adds an attribute — structured metadata attached to code, such as #[\Override] or #[\Deprecated]. It starts with # but it is not a comment. You'll meet attributes in later lessons.
Naming and style
Most PHP projects follow the PHP-FIG coding style (PSR-12 and its successor, PER Coding Style):
| Kind of name | Style | Example |
|---|---|---|
| Variables and properties | camelCase | $itemCount, $firstName |
| Functions and methods | camelCase | calculateTotal() |
| Classes, interfaces, traits, enums | PascalCase | ShoppingCart |
| Constants | UPPER_SNAKE_CASE | MAX_RETRIES |
Other key rules: four spaces for indentation, opening braces of functions and classes on their own line, and opening braces of if/for on the same line. Tools such as PHP CS Fixer or PHP_CodeSniffer apply these rules automatically. (Many of PHP's own older functions use snake_case, like str_replace — that's history, not a style to copy.)
Strict types
Modern PHP files often start with this line, right after <?php:
<?php declare(strict_types=1); function double(int $n): int { return $n * 2; } echo double(21), "\n"; echo double("21"), "\n";
42
Fatal error: Uncaught TypeError: double(): Argument #1 ($n) must be of type int, string given, called in main.php on line 11 and defined in main.php:5
Stack trace:
#0 main.php(11): double('21')
#1 {main}
thrown in main.php on line 5It tells PHP not to convert arguments automatically to the declared types — passing the string "21" where an int is expected becomes an error rather than a silent conversion. Lesson 29 explains type declarations; for now, know that it catches bugs early and is recommended for new code.
Try it yourself
Add a docblock to the function, and comments explaining each step.
Floor area: 14.7 m²