Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Understanding variables

Thursday, July 17th, 2014 by Servage

aaThere’s a simple metaphor that will help you understand what PHP variables are all about. Just think of them as little (or big) matchboxes! That’s right, matchboxes that you’ve painted over and written names on.

String variables

Imagine you have a matchbox on which you have written the word username. You then write Fred Smith on a piece of paper and place it into the box. Well, that’s the same process as assigning a string value to a variable, like this:

$username = "Fred Smith";

The quotation marks indicate that “Fred Smith” is a string of characters. You must enclose each string in either quotation marks or apostrophes (single quotes), although there is a subtle difference between the two types of quote, which is explained later.

When you want to see what’s in the box, you open it, take out the piece of paper, and read it. In PHP, doing so looks like this:

echo $username;

Or you can assign it to another variable (i.e., photocopy the paper and place the copy in another matchbox), like this:

$current_user = $username;

If you are keen to start trying out PHP for yourself, you could try entering the examples in this chapter into an IDE to see instant results, or you could enter the code into a program editor and save it to your server’s document root directory as test1.php.

<?php // test1.php
$username = "Fred Smith";
echo $username;
echo "<br />";
$current_user = $username;
echo $current_user;

Now you can call it up by entering the following into your browser’s address bar:


If during installation of your web server you changed the port assigned to the server to anything other than 80, you must place that port number within the URL in this and all other examples in this article. So, for example, if you changed the port to 8080, the preceding URL becomes:


I will not mention this again, so just remember to use the port number if required when trying out any examples or writing your own code.

The result of running this code should be two occurrences of the name “Fred Smith,” the first of which is the result of the echo $username command and the second of the echo $current_user command.

Numeric variables

Variables don’t contain just strings—they can contain numbers, too. Using the matchbox analogy, to store the number 17 in the variable $count, the equivalent would be placing, say, 17 beads in a matchbox on which you have written the word count:

$count = 17;

You could also use a floating-point number (containing a decimal point); the syntax is the same:

$count = 17.5;

To examine the contents of the matchbox, you would simply open it and count the beads. In PHP, you would assign the value of $count to another variable or perhaps just echo it to the web browser.


So what are arrays? Well, you can think of them as several matchboxes glued together. For example, let’s say we want to store the player names for a five-person soccer team in an array called $team. To do this, we could glue five matchboxes side by side.

Sources for further reading

Understanding variables, 5.0 out of 5 based on 2 ratings
Categories: Guides & Tutorials, Tips & Tricks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.