Himanshu SharmaSoftware engineer
Let’s talk

PHP · Lesson 5 of 54

Variables

Create PHP variables, follow the naming rules, assign and reassign values, swap them, and see what happens when you read a variable that was never set.

11 minThe basicsUpdated 22 October 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonCreate, name, change and print variables, and recognise an undefined-variable warning.

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
<?php
$productName = "Mechanical Keyboard";
$price = 89.50;
$inStock = 60;

echo $productName . "\n";
echo $price . "\n";
echo $inStock . "\n";
result
Mechanical Keyboard
89.5
60

The 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
<?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";
result
Start: 60
After a sale: 57
After a delivery: 77

Because 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
<?php
$value = 42;
echo gettype($value) . "\n";
$value = "forty-two";
echo gettype($value) . "\n";
result
integer
string

gettype() 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
<?php
$name = "Asha";
echo "Hello, $name!\n";
echo 'Hello, $name!' . "\n";
result
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
<?php
$first = "tea";
$second = "coffee";

[$first, $second] = [$second, $first];

echo "first = $first, second = $second\n";
result
first = coffee, second = tea

Variables 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
<?php
$quantity = 3;
echo "Quantity: $quantty\n";
echo "The script continues\n";
result
Warning: Undefined variable $quantty in /path/to/script.php on line 3
Quantity:
The script continues

The 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
<?php
$coupon = "SAVE10";
var_dump(isset($coupon));

unset($coupon);
var_dump(isset($coupon));

$discount = null;
var_dump(isset($discount));
result
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, and echo price; is a different error, because PHP looks for a constant called price.
  • 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
<?php
$product = "Ceramic Mug Set";
$price = 18.75;
$quantity = 4;
$total = $price * $quantity;

echo "$quantity x $product = $total\n";
result
4 x Ceramic Mug Set = 75

The 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.

Check yourself

1. Which of these is a valid PHP variable name?
2. What does this print? $a = 5; $a = $a + 2; echo $a;
3. What happens when you echo a variable that was never assigned?
4. What does isset($x) return after unset($x)?