What a variable is
A variable is a name for a value that your program wants to remember and use later: a price, a customer's name, the number of items in a cart. In PHP every variable name starts with a dollar sign.
<?php
$productName = "Mechanical Keyboard";
$price = 89.50;
$inStock = 60;
echo $productName . "\n";
echo $price . "\n";
echo $inStock . "\n";Mechanical Keyboard
89.5
60The single equals sign = is the assignment operator. It means "store the value on the right in the variable on the left". It does not mean "is equal to"; comparison uses == and ===, which have their own lesson.
Unlike many languages, PHP does not ask you to declare a variable or its type before using it. The first assignment creates it.
Naming rules
A variable name is $ followed by:
- a letter or an underscore first,
- then any mix of letters, digits and underscores.
So $price, $_total, $item2 and $orderTotal are valid, while $2items (starts with a digit), $order-total (hyphen) and $first name (space) are not. Names are case-sensitive: $total and $Total are different variables.
Choose names that say what the value is. $customerEmail is better than $ce or $data, because the code then explains itself. The two common styles are $camelCase and $snake_case. Pick one per project; this course uses camelCase.
Changing a variable
Assigning again replaces the old value. The right-hand side is always worked out first, so a variable can appear on both sides:
<?php
$stock = 60;
echo "Start: $stock\n";
$stock = $stock - 3; // three keyboards sold
echo "After a sale: $stock\n";
$stock = $stock + 20; // a delivery arrived
echo "After a delivery: $stock\n";Start: 60
After a sale: 57
After a delivery: 77Because PHP is dynamically typed, the same variable can hold a number now and a piece of text later. That is allowed, but it is usually a sign of confusing code. Keep one meaning per variable.
<?php
$value = 42;
echo gettype($value) . "\n";
$value = "forty-two";
echo gettype($value) . "\n";integer
stringgettype() reports the current type of a value. Types are the subject of the next lesson.
Printing variables inside text
When text is written in double quotes, PHP replaces variable names with their values. In single quotes the text is used exactly as written:
<?php
$name = "Asha";
echo "Hello, $name!\n";
echo 'Hello, $name!' . "\n";Hello, Asha!
Hello, $name!The strings lesson covers this "interpolation" in detail, including the curly-brace form for more complex expressions.
Swapping two values
A classic exercise: swap the values of two variables. You need a temporary third variable, or PHP's array destructuring, which swaps in one line:
<?php
$first = "tea";
$second = "coffee";
[$first, $second] = [$second, $first];
echo "first = $first, second = $second\n";first = coffee, second = teaVariables that do not exist
If you read a variable that was never assigned, PHP 8 prints a warning and carries on, using the value null. The warning is there to catch typing mistakes:
<?php
$quantity = 3;
echo "Quantity: $quantty\n";
echo "The script continues\n";Warning: Undefined variable $quantty in /path/to/script.php on line 3
Quantity:
The script continuesThe typo $quantty was spotted, but notice that the script did not stop: it printed an empty value and continued. In older PHP versions this was only a "notice" that many servers hid, which is how bugs slipped through. Treat every warning as a bug to fix.
isset and unset
isset() asks whether a variable exists and holds something other than null. unset() deletes a variable.
<?php
$coupon = "SAVE10";
var_dump(isset($coupon));
unset($coupon);
var_dump(isset($coupon));
$discount = null;
var_dump(isset($discount));bool(true)
bool(false)
bool(false)var_dump() prints a value together with its type, which is why the output says bool(true) rather than just 1. You will use isset() constantly when handling forms, where a field may or may not have been sent.
Common mistakes
- Forgetting the $:
price = 10;is a syntax error, andecho price;is a different error, because PHP looks for a constant calledprice. - Typos in names, such as
$quantty, which create a new, empty variable. Read the undefined-variable warning carefully. - Expecting single quotes to insert values.
'Total: $total'prints the dollar sign and the name. - Reusing one variable for unrelated things, which makes code hard to follow.
Exercise
Create variables for a product name (Ceramic Mug Set), a price (18.75) and a quantity (4). Calculate the total cost in a fourth variable and print one line: 4 x Ceramic Mug Set = 75.
Show one possible answer
<?php
$product = "Ceramic Mug Set";
$price = 18.75;
$quantity = 4;
$total = $price * $quantity;
echo "$quantity x $product = $total\n";4 x Ceramic Mug Set = 75The total is the float 75.0, which PHP prints as 75 because a float with no fractional part is shown without decimals.
Key points
- A variable is a named container for a value. Its name starts with $ followed by a letter or underscore.
- = assigns a value. Assigning again replaces the old value; the type can change too.
- PHP variables do not need to be declared first, but reading one that was never set gives a warning and the value null.
- isset() tells you whether a variable exists and is not null; unset() removes it.