Skip to content
Theory · Variables and types · lesson 1 of 6

Variables

What you'll learn

What you'll learn:

  • create variables and change their values;
  • explain why changing $a after $b = $a does not change $b;
  • find a value's type with var_dump and predict what echo prints;
  • convert values from one type to another and declare constants.
Two jars labelled $a and $b; a copy of the tile 5 moves into the second while the original stays

Picture a jar with a label. The label holds a name, and inside the jar is a value. You can find the jar by its name, look inside, or put something else in it. A program keeps its data the same way—in variables: named values you can read and change.

You already met one variable in the first theme: the parameter $x of the double function. Now let's create variables ourselves. A variable lets you remember a value once and then use it by name:

PHP
<?php

$name = 'Anna';
$age = 30;
echo $name, ' ', $age, "\n"; // Anna 30
$age = $age + 1;
echo $name, ' ', $age, "\n"; // Anna 31

Let's go through it line by line:

  1. $name = 'Anna'; creates the jar $name and puts the string 'Anna' in it.
  2. $age = 30; creates a second jar, holding the number 30.
  3. echo prints what both jars hold: Anna 30.
  4. $age = $age + 1; reads 30 from $age, adds 1 and puts the result 31 back into the same jar.
  5. The second echo prints Anna 31: nothing changed in $name.

You don't declare a variable in advance. It appears the moment you first put a value into it.

Variable names

A variable name in PHP always starts with the $ sign. The sign is part of the name: to PHP, $age and age are different things, and there is no variable without $.

The rules for the rest of the name:

  • the first character after $ is a letter or an underscore, not a digit;
  • then come letters, digits and underscores;
  • case matters: $age and $Age are two different variables.
PHP
$Age = 40;
echo $age, ' ', $Age, "\n"; // 31 40

Here $Age is a new variable, and $age still holds 31. In this course, compound names are written as one word, with each word after the first starting with a capital letter: $userName, $itemCount. Most PHP projects follow this style.

Assignment

The = sign is assignment: PHP evaluates the right side and puts the result into the variable on the left. It is not equality from math. Read $age = $age + 1 like this: "put into $age what it held, plus one".

A new assignment replaces the old value. The previous one is gone: a jar always holds exactly one thing.

The jar analogy is not exact here. If you take the jam out of a real jar, the jar is empty. Reading a variable means looking at its value, not taking it away: after echo $age, $age holds the same number.

A copy on assignment

So what does the line $b = $a do? It does not stick a second label on the jar $a. PHP takes a copy of what $a holds and puts it into a separate jar $b. From then on each variable has its own value: changing $a does not change $b.

Let's check this on a program. Click "Next step" and watch $b and $greeting in the {main} frame: after their assignment they no longer change.

LiveAssignment copies the valuestep 1 of 8 · frames: 1

The number 5 goes into the jar $a.

Watch $b and $greeting in the {main} frame

Both pairs of variables behave the same way:

  1. $b = $a copies the number 5, and after $a = $a + 1, $b still holds 5.
  2. $greeting = $name copies the string 'Anna', and the new value of $name never reaches $greeting.
  3. That is why the program prints 6 5 and Anna.

Interviewers like to ask this: "What ends up in $b after $b = $a and a change to $a?" The answer: the value $a held at the moment of assignment. Assignment copies the value; it does not link the two variables.

Common mistake

A variable name was written with a typo:

PHP
$total = 10;
echo $totl, "\n"; // ✗
TEXT
Warning: Undefined variable $totl

Nothing was ever assigned to $totl, and PHP warns about it. But the script does not stop: nothing is printed in place of the value, and the lines below keep running. So read warnings even when the program runs to the end. The fix is the same name as in the assignment: echo $total, "\n";.

Key points

  • A variable is a named value; its name starts with $, and case in the name matters.
  • = puts the value on the right into the variable on the left; $age = $age + 1 writes a new value into the same variable.
  • $b = $a copies the value at the moment of assignment: a later change to $a does not change $b.
  • Reading a variable that was never assigned gives Warning: Undefined variable, but the script keeps running.