1.1Variables, types & operators
The essentials in 30 seconds
- Variable = $ + clear name, dynamic typing: $prixTotal = 1200; — PHP infers the type from the value (string, int, float, bool, array, null, object).
- Concatenate with the DOT, never +: 'Hello ' . $nom — + does not join strings in PHP.
- Compare STRICT by default: === (value AND type, predictable) rather than == (automatic conversion, bugs and vulnerabilities).
Running mini-case — Riad, web developer in Constantine
Riad, a web developer in Constantine, prepares a quote for a showcase website: he stores the ex-VAT price in $prixHT, computes VAT with arithmetic operators, then displays "Total: " followed by the amount. In a hurry, he joins the message with + and compares the entered promo code ("5") to the number 5 with ==: the display shows 0 instead of the text, and an unexpected promo code passes the comparison. He fixes it by concatenating with the dot and comparing with ===: the quote displays correctly and only genuine promo codes are accepted.
This same case returns in the lesson, diagram, script and 48h challenge — and continues in 1.2, 1.3, 1.4 and 1.5.
1. Variables & types
A variable always starts with $ ($nom), assigned with = ($age = 30;). PHP has dynamic typing: the type follows the value, with no prior declaration — a variable can change type as the code runs. Types to know: string (text), int (integer), float (decimal), bool (true/false), array, null, object. Name your variables clearly ($prixTotal rather than $x): explicit naming is a key to readability.
Riad stores $prixHT = 10000; and $client = "Yasmine";: names that say what they hold, re-read effortlessly three weeks later when the client asks for a change.
2. Operators, === and quotes
Operators: arithmetic (+ - * / % **), assignment (=, +=…), comparison, logical (&& || !). Concatenation uses the DOT . (not +!). CRUCIAL: == compares with type conversion ("loose", surprising results); === compares value AND type ("strict") — prefer ===. Double quotes interpret variables ("Hello $nom"); single quotes = literal ('Hello $nom').
Riad replaces his + with a . to join "Total: " and the amount, and his == with === for the promo code: the display becomes correct and the comparison predictable.
🔎 Concrete example — 🔎 Concrete example — === vs == and quotes
Avoid surprises, like Riad on his quote.
$nom = 'Riad';
echo "Hello $nom"; // Hello Riad (interpreted)
echo 'Hello $nom'; // Hello $nom (literal)
$message = 'Total: ' . 12000; // dot concatenation
echo $message;
var_dump('5' == 5); // true (loose conversion)
var_dump('5' === 5); // false (different type)
Reading: doubles display the value, singles the raw text; the dot joins, === locks the type.
Lesson: predictable comparisons and controlled display — the === by default reflex.
Section vocabulary
- Variable ($)
- Stores a piece of data; always starts with $ ($nom), assigned with =. Name is case-sensitive, to be chosen clear and meaningful.
Simple example: Riad's $prixHT and $client on the quote.
What it is not: a cryptic $x whose content nobody can guess.
Reflex: always $, explicit name, consistent casing.
- Dynamic typing
- PHP determines the type from the value (no prior declaration); a variable can change type. Types: string, int, float, bool, array, null, object.
Simple example: Riad's $prixHT, an integer then a decimal after VAT.
What it is not: declaring the type upfront as in a compiled language.
Reflex: think value first, check with var_dump when in doubt.
- Operators
- Arithmetic (+ - * / % **), assignment (=, +=…), comparison (==, ===, !=, <, >…), logical (&& || !).
Simple example: Riad's VAT computation ($prixHT * 1.19).
What it is not: using + to join two strings.
Reflex: compute with operators, join with the dot.
- Concatenation (.)
- In PHP, strings are joined with the DOT . (not +): 'Hello ' . $nom.
Simple example: The "Total: " . $montant displayed by Riad.
What it is not: 'Total: ' + $montant, which computes instead of joining.
Reflex: strings to join = dot, always.
- == vs === / quotes
- == compares with type conversion (loose, surprising); === compares value AND type (strict, preferred). Doubles interpret variables, singles = literal.
Simple example: The promo code tested with === by Riad; his message in doubles.
What it is not: validating a sensitive token with ==.
Reflex: === by default; doubles to interpret, singles otherwise.
In practice — In practice — Handling variables, types and operators
- Declare variables of different types (string, int, float, bool) with `$` and display them; observe dynamic typing.
- Do computations with arithmetic operators (including % modulo) and concatenate strings with the DOT `.` (not `+`).
- Compare with `==` then `===` on a string and a number (e.g. '5' and 5): observe the difference (conversion vs strict) and prefer `===`.
- Test quotes: `"Hello $nom"` (interprets the variable) vs `'Hello $nom'` (literal) — notice the difference.
Ready-to-say script — justifying === in code review (30 s)
"I suggest we compare with === everywhere here: with ==, PHP converts types and an unexpected promo code can pass. Strict makes the comparison predictable, and for sensitive values it is also a security matter."
Say in code review; show var_dump('5' == 5) versus ===.
Copy-paste template — variables, concatenation, strict comparison
Quote in Constantine
Like Riad: ex-VAT price, VAT and total in named variables, message joined with the dot — the client re-reads a clean quote, with no fanciful figure.
Promo code to validate
Compare the entered value to the expected code with ===: no surprise conversion, only exact codes pass.
Display with variables
Doubles when the text holds variables ("Hello $nom"), singles for pure text — with explicit concatenation as an alternative.
Doubt about a type
A var_dump($variable) locally settles it: dynamic typing is checked, not guessed.
✓ Best practices
- Prefer === / !== by default (strict, predictable comparison).
- Concatenate strings with the dot (never +).
- Name variables clearly and stay consistent (casing).
✗ Common pitfalls
- Using == and suffering a surprising type conversion.
- Concatenating with + (does not exist for strings in PHP).
- Expecting 'Hello $nom' (single quotes) to display the value.
Key takeaways
- A VARIABLE always starts with `$` (`$nom`), assigned with `=`. PHP has DYNAMIC TYPING (type follows the value). Types: string, int, float, bool, array, null, object.
- String CONCATENATION with the DOT `.` (NOT `+`!). Operators: arithmetic (+ - * / % **), assignment (+=…), comparison, logical (&& || !).
- CRUCIAL: `==` compares with type conversion (loose, surprising); `===` compares value AND type (strict) — PREFER `===` / `!==`.
- DOUBLE quotes interpret variables (`"Hello $nom"`); SINGLE quotes = literal (`'Hello $nom'`). Name variables clearly (readability).
48h challenge — apply tomorrow
Take one of your scripts (template above): replace every == with === (except wanted conversion), every + between strings with a dot, and rename one cryptic variable. Run locally and check the display. 20 minutes is enough.
- I can explain $, dynamic typing and dot concatenation.
- I compare with === by default and I know why.
- I choose doubles (interpret) vs singles (literal) without hesitating.
Frequently asked questions
Why prefer === over == in PHP, and which bugs does that avoid?
`==` compares two values with AUTOMATIC type conversion when types differ ("loose" comparison), which gives surprising, counter-intuitive results. `===` compares value AND type, WITHOUT conversion ("strict"), hence predictable. Bugs `==` can cause: comparing a string and a number (confusing conversions), the "falsy" value traps (0, '', '0', null, [] considered "false"), and above all dangerous cases — historically, hash or token comparisons wrongly done with `==` caused security vulnerabilities. PHP 8 fixed some absurd cases, but the principle stands: `==` converts, `===` does not. Good practice: use `===` (and `!==`) by DEFAULT; if you must compare mixed types, CONVERT explicitly first (control) rather than suffering implicit conversion.
Single or double quotes: what is the difference and which to use?
DOUBLE quotes `"..."`: INTERPRET the content — variables (`"Hello $nom"` inserts the value) and escape sequences (`\n`, `\t`). SINGLE quotes `'...'`: take the content LITERALLY — `'Hello $nom'` displays the text "Hello $nom" (not the value). Best practices: SINGLE quotes by default for pure text (more predictable); DOUBLE quotes when you want to interpret variables; recommended alternative: CONCATENATION with the dot (`'Hello ' . $nom`), explicit and predictable. Caution: an accidental `$` in double quotes can cause unexpected behaviour.
Which comparison to prefer in modern PHP?