Variables
Variables store values so you can use them later. In PHP every variable name starts with a dollar sign, you create a variable simply by assigning to it, and a variable can hold any type of value — PHP works out the type from the value. This lesson covers naming, assignment, checking whether variables exist, and how assignment copies values.
Creating variables
There's no separate declaration step: assigning a value creates the variable.
<?php $name = "Priya"; $age = 31; $height = 1.68; $isMember = true; echo "$name is $age years old and $height m tall.\n";
Priya is 31 years old and 1.68 m tall.
Variable names start with $, then a letter or underscore, followed by letters, digits or underscores. $total2 and $_count are fine; $2total isn't. Names are case-sensitive.
Changing values — and types
A variable can be reassigned at any time, even to a value of a different type. PHP is dynamically typed: the type belongs to the value, not the variable.
<?php $score = 10; $score = $score + 5; $score += 20; // shorthand for $score = $score + 20 echo $score, "\n"; $value = 42; var_dump($value); $value = "forty-two"; var_dump($value);
35 int(42) string(9) "forty-two"
Changing a variable's type is legal, but it makes code harder to follow. Keep each variable to one kind of value; type declarations (lesson 29) help enforce that for function parameters and properties.
Undefined variables
Reading a variable that was never assigned gives a warning, and the value is treated as null:
<?php $total = $subtotal + 5; echo $total, "\n";
Warning: Undefined variable $subtotal in main.php on line 3 5
Most of the time this warning means a typo or a missing assignment. Fix the cause rather than hiding the warning.
isset, empty and unset
isset() checks whether a variable exists and isn't null; it never warns. empty() checks whether it's missing or "falsy" (such as 0, "" or null). unset() deletes a variable.
<?php $city = "Oslo"; $count = 0; $nothing = null; var_dump(isset($city), isset($nothing), isset($missing)); var_dump(empty($count), empty($city)); unset($city); var_dump(isset($city));
bool(true) bool(false) bool(false) bool(true) bool(false) bool(false)
Be careful with empty(): the string "0" also counts as empty, which surprises people validating form input. Explicit checks like $x === '' are often clearer.
Null and the ?? operator
null means "no value". The null-coalescing operator ?? gives a fallback for a variable that is null or doesn't exist — without a warning:
<?php $nickname = null; echo $nickname ?? "(no nickname)", "\n"; echo $undefinedVar ?? "(not set)", "\n"; $theme = null; $theme ??= "light"; // assign only if null or unset echo $theme, "\n";
(no nickname) (not set) light
Assignment copies values
Assigning one variable to another copies the value. Changing one afterwards doesn't affect the other — even for arrays:
<?php $a = 10; $b = $a; $b = 99; echo "a = $a, b = $b\n"; $original = [1, 2, 3]; $copy = $original; $copy[] = 4; echo count($original), " vs ", count($copy), "\n";
a = 10, b = 99 3 vs 4
Objects are different: assigning an object copies a handle to the same object (lesson 33).
References
Putting & before the value makes a reference: two names for the same variable. Changing one changes the other:
<?php $price = 100; $alias = &$price; $alias = 80; echo $price, "\n";
80
References are occasionally useful (lesson 28 shows passing by reference), but they make code harder to reason about. Prefer returning new values.
Swapping and multiple assignment
Array destructuring (lesson 26) assigns several variables at once — including swapping two values without a temporary variable:
<?php [$x, $y] = [1, 2]; echo "$x $y\n"; [$x, $y] = [$y, $x]; echo "$x $y\n";
1 2 2 1
Variable variables
PHP lets a variable's name come from another variable with $$. You'll see this in old code, but an array is almost always a better choice:
<?php $field = "colour"; $$field = "green"; // creates $colour echo $colour, "\n";
green
Try it yourself
Calculate the total cost of an order. Then try removing the $quantity line to see the warning.
4 x Notebook = 9.96