PHP Variables
A variable is a named container for a value. In PHP every variable name starts with a dollar sign $, followed by the name: $city, $price, $isMember.
Creating variables
There is no special keyword to declare a variable. You create it the first time you assign a value with =:
<?php
$city = "Lisbon";
$temperature = 23;
$isSunny = true;
echo "City: $city\n";
echo "Temperature: $temperature degrees\n";
echo "Sunny: " . ($isSunny ? "yes" : "no") . "\n";City: Lisbon
Temperature: 23 degrees
Sunny: yes
PHP is dynamically typed: you never write the type of a variable. PHP works out the type from the value, and a variable can even hold a different type later.
Naming rules
| Rule | Valid | Invalid |
|---|---|---|
Starts with $ |
$age |
age |
First character after $ is a letter or _ |
$_count, $total2 |
$2total |
Only letters, digits and _ |
$first_name |
$first-name, $first name |
| Case-sensitive | $Score ≠ $score |
— |
Common styles are camelCase ($orderTotal) and snake_case ($order_total). Pick one and stick with it; most modern PHP code uses camelCase for variables.
Note:
$thisis reserved for objects (you will meet it in the OOP section), so you cannot use it as an ordinary variable name.
Changing values
Assigning again replaces the old value. You can also update a variable based on its current value:
<?php
$score = 10;
echo "Start: $score\n";
$score = $score + 5;
echo "After bonus: $score\n";
$score += 20; // shorthand for $score = $score + 20
echo "After level up: $score\n";
$score = "game over"; // now it holds a string
echo "Finally: $score\n";Start: 10
After bonus: 15
After level up: 35
Finally: game over
Outputting variables
There are two common ways to put a variable into text:
- Interpolation inside double quotes:
"Hello $name" - Concatenation with the dot operator:
'Hello ' . $name
Single-quoted strings do not interpolate variables:
<?php
$fruit = "mango";
$count = 3;
echo "I bought $count {$fruit}s\n"; // braces separate the name from the "s"
echo 'I bought $count $fruit' . "\n"; // printed literally
echo 'I bought ' . $count . ' ' . $fruit . "s\n";I bought 3 mangos
I bought $count $fruit
I bought 3 mangos
Curly braces {$fruit} tell PHP exactly where the variable name ends, which is necessary when letters follow directly.
Several variables at once
You can assign the same value to multiple variables in one statement, or unpack an array into variables:
<?php
$red = $green = $blue = 0;
echo "rgb($red, $green, $blue)\n";
[$width, $height] = [1920, 1080];
echo "Screen: {$width}x{$height}\n";rgb(0, 0, 0)
Screen: 1920x1080
Checking whether a variable exists
Reading a variable that was never assigned produces a warning and gives null. Use isset() to check first, or the null coalescing operator ?? to supply a default:
$nickname = $nickname ?? "guest"; // "guest" if $nickname is unset or null
if (isset($discount)) { /* ... */ }
unset($nickname); // removes the variable| Function | Purpose |
|---|---|
isset($v) |
true if $v exists and is not null |
empty($v) |
true if $v is missing or "falsy" (0, "", [], null, false) |
unset($v) |
Destroys the variable |
var_dump($v) |
Shows the type and value — great for debugging |
Tip:
var_dump()is your best friend when something behaves unexpectedly. It shows both the type and the value, e.g.int(23)orstring(6) "Lisbon".
Test yourself
Which variable name is **valid**?
$2fast$first-name$_totaltotal
With $n = 5;, what does echo 'Value: $n'; print?
Value: 5Value: $nValue:- An error
What does $name = $name ?? "guest"; do?
- Always sets
$nameto "guest" - Sets
$nameto "guest" only if it is unset or null - Compares
$namewith "guest" - Deletes
$name