PHP: Variables & Data Types
What you will learn
How to declare variables (they always start with $), PHP's dynamic typing, and the strangeness of loose comparison.
PHP is dynamically typed — a variable can hold a string now and a number later. Variables must start with $ followed by a letter or underscore.
$name = "Alice"; // string
$age = 25; // int
$height = 5.6; // float
$isStudent = true; // bool
Variable naming rules
- Must start with
$then a letter or_ - Case-sensitive:
$Nameand$nameare different - Convention:
$snake_case
Dynamic typing in action
PHP converts types automatically in many contexts:
$result = "5" + 3; // 8 (string "5" converted to int)
$result = "5" . 3; // "53" (. is concatenation)
The + operator coerces strings to numbers. The . operator concatenates (coerces numbers to strings).
Loose vs strict comparison
var_dump(5 == "5"); // bool(true) — loose, coerces types
var_dump(5 === "5"); // bool(false) — strict, checks type too
Use === (identical) to avoid surprises. This is one of PHP's most common gotchas.
Common mistakes
- Forgetting
$at the start of a variable name — PHP will treat it as a constant - Relying on loose
==comparison —"0" == falseis true! - Thinking
.is the decimal separator in strings — use.for concatenation, not+
Quick check below!