Variables & Data Types
Prerequisites: How PHP code runs
PHP variables start with a $ sign and don't need a declared type —
the type is determined by the value assigned to them.
Syntax
$name = "Ada"; // string
$age = 36; // int
$height = 1.7; // float
$isActive = true; // bool
$tags = ["php", "web"]; // array
$nothing = null; // null
Example
$score = 10;
echo $score;
$score = 20;
echo $score;
Expected output
1020
Use gettype($variable) to check a value's type at runtime, and
var_dump($variable) to inspect both its type and value while
debugging.
Common mistakes
- Forgetting the
$sigil —name = "Ada";is a syntax error, since PHP needs$name. - Expecting
echoto add a newline automatically — it doesn't; the output above runs together on one line without an explicit\n.
Practice
Declare a variable called city holding your city's name, then print
it.
Show solution
$city = "Lagos";
echo $city;