Those of you who have never done any programming may find variables a bit difficult to understand at first. I'll try to explain them at a basic level. Let me first make a comparison. When speaking in English, we often use non-specific pronouns like "he," "she," "it," and "they" in the place of the real objects they represent. It would be cumbersome to always use all of the proper names of everything. In one instance, the pronoun "he" might mean "Steve," and in another instance, it might mean "Gary." The pronoun "he" is a variable that takes on a specific meaning depending on the context
Variables in PHP are kind of like these non-specific pronouns. They represent real objects, but they themselves are just generic placeholders, rather than the objects themselves.
As a programmer, you get to make up the names of your own variables. If you want to create a variable that keeps track of colors, you can call the variable "color" if you want to. That would make sense. In PHP, you designate a variable by putting a dollar sign in front of it: $color. There are a few things to consider when making up variable names:
Once you decide on a name, you get to assign an initial value to the variable. For example:
$color = 'red';
Notice that there is a semi-colon at the end of the line. This is required. You must end each programming statement with a semi-colon so the software knows where one statement ends and the next begins.
By themselves, variables are rather useless, but as soon as we apply them to contexts, their usefulness increases. To show the value of a variable, we have to "echo" the value. I don't know why they chose the word "echo," but that's the word they chose, so that's what we have to use. Here is an XHTML paragraph that makes use of our $color variable:
<p>Today, I'm going to wear a <?php echo $color; ?> tie.</p>
The above construction allows us to substitute any color we want in that sentence. If we set $color = 'blue'; the sentence will say "Today I'm going to wear a blue tie." The sentence never has to change. Only the color changes.
The web server processes the PHP code and puts in the variable value in its place, so the end result in the code looks as if no PHP was used at all. Using our color example, the end result, from the perspective of the browser looks like this:
<p>Today, I'm going to wear a blue tie.</p>
All traces of the PHP code are gone.
When we apply this idea to templates, it allows us to use variables for things like the <title> of the page and the main heading:
<title><?php echo $title; ?></title>
and:
<h1><?php echo $h1; ?></h1>
Of course, we have to set the value of the variables previously, or else there won't be anything to put there.
You have to be careful when using things like quotation marks or apostrophes when working with PHP, because quotation marks (either single or double quotation marks) mark the beginning and ending of values. If you have an apostrophe or quotation mark in the middle of a value, PHP will think it has reached the end of the value. For example, if you write:
<?php $lastname = 'O'Niel'; ?>
That won't work because the apostrophe after the "O" will be interpreted as the end of the value. There are a couple of ways to get around this:
1. Use the opposite kind of quotation mark (double vs. single):
<?php $lastname = "O'Niel"; ?>
OR
Type a backslash before the apostrophe, like this:
<?php $lastname = 'O\'Niel'; ?>
The first method is usually easier to read, but either method will work. It's still best to use single quotation marks whenever possible for variable values, because of the way PHP processes text. Text in single quotes is interpreted as plain text and is processed very quickly. Text in double quotes may be plain text or it may be PHP code, so the server processes this text more carefully and slowly, which has a slight negative effect on the speed of the page. For simple pages the difference won't be noticeable at all. It only makes a noticeable difference with very large scripts.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.