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
<?php

$hour = 14;

if ($hour < 12) {
    echo "Good morning\n";
    echo "Coffee time.\n";
} else {
    echo "Good afternoon\n";
}
Output
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: $total and $Total are different variables.
  • Keywords, functions and class names are not: ECHO, StrLen() and strlen() all work.
PHP
<?php

$color = "red";
$Color = "blue";
echo $color, " ", $Color, "\n";

ECHO STRTOUPPER("works anyway"), "\n";
Output
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
<?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
Output
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
<?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";
Output
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 nameStyleExample
Variables and propertiescamelCase$itemCount, $firstName
Functions and methodscamelCasecalculateTotal()
Classes, interfaces, traits, enumsPascalCaseShoppingCart
ConstantsUPPER_SNAKE_CASEMAX_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
<?php

declare(strict_types=1);

function double(int $n): int
{
    return $n * 2;
}

echo double(21), "\n";
echo double("21"), "\n";
Output
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 5

It 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.

Try it yourself · main.php
Edit the code, copy it into main.php, then run it with php main.php (PHP 8.5). How to install PHP
Output
Floor area: 14.7 m²