A variable is used in PHP scripts to represent a value. As the name variable suggests, the value of a variable can change (or vary) throughout the program. Variables are one of the features that distinguish a programming language like PHP from markup languages such as HTML.
Variables allow you write your code in a generic manner. To highlight this, consider a web form which asks users to input their name and favorite color.
Every time the form is completed, the data will be different. One user may say his name is John and his favorite color is blue. Another may say her name is Susan and her favorite color is yellow. We need a way of working with the values a user enters. The way to do this is by using variables.
There are a few standard variables that PHP creates automatically, but most of the time variables are created (or declared) by you, the programmer. By creating two variables called
$color, you could create generic code which can handle any input values. For John, this code:
<?php echo "Hello, $name. Your favorite color is $color.";
Hello, John. Your favorite color is blue.
On the other hand, Susan would see:
Hello, Susan. Your favorite color is yellow.
We will look into variable names and displaying the value of variables in the rest of this article, but the important point now is to understand how the use of generic variables can make processing data easy.
In PHP, simply writing the name of a variable for the first time in a script will create it. There’s nothing extra you need to do. The variable name has to follow some standard rules, though:
- The name starts with a
- The first character after the
$must be a letter or underscore
- All subsequent characters can be a combination of letters, numbers and underscores
$customerName is a valid variable name because it observes all three rules above.
$123customer is not valid because it violates the second rule, the first character after the
$ sign must be a letter or underscore.
It’s a good idea to give your variable a meaningful name. If the data you will be storing is a customer’s name, a sensible name might be
$customerName. You could also call it
$abc123, but I hope you agree that the former suggestion is better.
There are different conventions you can follow when writing variable names. Regardless of what you choose, it is important to be consistent and follow the convention throughout your script. For example, you can use an underscore to separate words (e.g.
$customer_name), or use capital letters to differentiate words, a style called Camel Case (e.g.
You are allowed to use both upper and lower case letters when naming a variable, but be aware that
$CustomerName is not the same as
$customerName. PHP will treat the two as different variables! This reinforces the need to stick to a naming convention.
Now that you know you can have PHP create a new variable anytime you need it just by writing a new name, let’s look at another example to learn about assigning values to them.
<?php $customerName = "Fred"; $customerID; $customerID = 346646; $customerName = $customerID;
First, the variable
$customerName is given the value “Fred”. This is referred to as assigning a value. And because this is the first time
$customerName is used, the variable is created automatically. Anytime you write
$customerName after that, PHP will know to use the value “Fred” instead.
$customerID is written. A variable can be created without assigning a value to it, though this is generally not considered to be good practice. It is better to assign a default value just so you know it has a value. Remember, variables are variable, so you can always change the value later. Afterwards, the variable
$customerID is assigned the value 346646.
Finally, the value of
$customerID is assigned to
$customerName. You can assign the value of one variable to another; in this case the value of
$customerID (346646) overwrites the value in
$customerName (“Fred”) so that both variables now represent 346646!
Notice how the types of data referenced by a variable have different “types.” This property is called the data’s data type. “Fred” is given with quotation marks, so it is a string (string is just a fancy name for text). 346646 is obviously a number (more specifically, an integer).
Here are some examples of assigning values with different data types:
<?php $total = 0; // Integer $total = "Year to Date"; // String $total = true; // Boolean $total = 100.25; // Double $total = array(250, 300, 325, 475); // Array
Now that you understand the basics of naming variables and assigning values, let’s look at this example and see if you can work out the answer:
<?php $firstNumber = 4; $secondNumber = 6; $result = $firstNumber + $secondNumber;
The examples in the previous section showed that values to the right of the
= sign are assigned to the variable name on the left of the
= sign, so the value 4 is assigned to
Have a close look at the last line. Though I haven’t explained it previously, the
+ sign is an operator, in this case performing addition. So what do you think the value will be in
If your answer is 10 then well done, that’s correct! If not, have a look at the example again and read the explanation carefully.
Displaying the Value of Variables
As you saw at the beginning, you can display the value represented by a variable using
echo. You can also use
<?php echo $customerName;
Perhaps you would like to make the example more meaningful by adding some text in quotation marks before the variable contents:
<?php echo "Customer name = " . $customerName;
The dot between the text in quotation marks and the variable name is the concatenation operator. It joins the string and the value of the variable together.
You could avoid using concatenation and make use of interpolation instead. Interpolation is when the variable name appears in a string and is replaced by its value instead. Taking advantage of this can sometimes make your code easier to read.
<?php echo "Customer name = $customerName";
PHP automatically performs interpolation on strings that are enclosed by double-quotation marks. If you wish to display the name of the variable with your text, you can use a backslash immediately before the variable name:
<?php echo "$customerName has the value: $customerName";
Alternatively, PHP will not perform interpolation on strings that are enclosed by single-quotation marks. So this is an equally effective statement:
<?php echo '$customerName has the value: ' . $customerName;
For more information about variables, check out the PHP documentation. You’ll review everything you’ve learned here, and learn about what special variables PHP will automatically define and make available to your scripts, how a variable is bound to the context in which it was declared, and even how variables can be used as the names for other variables!
Iain Tench has worked in the IT industry for 30 years as a programmer, project manager (Prince2 Practitioner), consultant, and teacher. As a project manager, he specialized in integration projects mainly involving payment systems in the financial sector. He now teaches and has acquired a Masters degree in Internet Systems Development as well as a teaching qualification. Iain's specialized areas of teaching is web technologies, mainly programming in a variety of languages, HTML, CSS and database integration. He's also written a book about Dreamweaver CS3 for Hodder Education.
The Principles of Beautiful Web Design, 4th Edition
Docker for Web Developers
HTML5 Games: Novice to Ninja