Variables
A variable in PHP is a named container for a value that can change while your script runs. Unlike languages such as Java or C#, PHP variables are not declared with a type ahead of time — you simply assign a value, and PHP figures out the type for you. Every variable name starts with a dollar sign ($), which is what tells the parser "this is a variable" rather than a bare word or function name.
Declaring and assigning
There is no separate "declaration" step in PHP — assignment is declaration. The first time you write to $name, that variable comes into existence.
Basic assignment
<?php
$name = "Aisha";
$age = 29;
$isActive = true;
$price = 19.99;
echo "{$name} is {$age} years old.";Aisha is 29 years old.
Dynamic typing
A variable's type is determined by whatever value it currently holds, and that type can change over the variable's lifetime. This is called dynamic typing. It is convenient for quick scripts but can hide bugs in larger codebases if you are not deliberate about it.
The same variable, different types
<?php $value = 10; // int var_dump($value); $value = "10"; // now a string var_dump($value); $value = 10.5; // now a float var_dump($value);
int(10) string(2) "10" float(10.5)
Naming rules
A variable name must start with a letter or underscore, never a digit:
$_countis valid,$1countis not.After the first character, letters, digits, and underscores are all allowed:
$user_2is valid.PHP variable names are case-sensitive:
$totaland$Totalare two different variables.There is no fixed length limit in practice, but short, meaningful names read better than cryptic abbreviations.
Valid vs invalid names
<?php $user_name = "ok"; // valid $_hidden = "ok"; // valid, starts with underscore $Price2 = "ok"; // valid, digit is fine after first char // $2fast = "no"; // invalid: cannot start with a digit // $user-name = "no"; // invalid: hyphen is not allowed in a name
Variable interpolation in strings
Double-quoted strings and heredocs will substitute variables directly;
single-quoted strings will not. The modern, unambiguous way to embed
a variable (especially one followed by letters, or a property/array
access) is curly-brace interpolation: "{$variable}".
Single vs double quotes vs curly interpolation
<?php
$user = ["name" => "Marco"];
$count = 3;
echo 'Hello $user[name]'; // literal text, no interpolation
echo "\n";
echo "Hello {$user['name']}"; // interpolated: Hello Marco
echo "\n";
echo "You have {$count}apples"; // curly braces avoid ambiguityHello $user[name] Hello Marco You have 3apples
isset(), unset(), and empty()
Three functions come up constantly when working with variables that might not exist yet: isset() checks whether a variable is set and is not null; unset() destroys a variable entirely; empty() checks whether a variable is either unset or holds a "falsy" value (covered in depth on the Booleans page).
isset, unset, and empty in action
<?php $city = "Toronto"; var_dump(isset($city)); // bool(true) var_dump(isset($country)); // bool(false) - never assigned unset($city); var_dump(isset($city)); // bool(false) - destroyed $votes = 0; var_dump(isset($votes)); // bool(true) - it exists var_dump(empty($votes)); // bool(true) - but 0 counts as empty
bool(true) bool(false) bool(false) bool(true) bool(true)