So far, all we've done is have a PHP script print some text. Big whoop.
Let's get down and dirty now with variables, which are like the wind beneath
the wings of the soaring eagle that, um, is PHP.
A variable is a container for holding one or more values. It is the means
by which PHP stores information and passes it along between documents and
functions and such. You may remember variables from algebra in the equation
"x + 2 = 8", x is a variable with the value 6.
The reason why variables are so important to PHP is that the very notion
of having dynamic Web pages pages which respond somehow to user input
relies on data being passed around between pages (or parts of a page).
Variables are the main mechanism for transferring data like this.
I think the easiest way to explain how variables work in PHP is to show
them in action. There are three basic things you can do with variables:
- Set them (give them one or more values);
- Re-set them if they were set before;
- Access them (read the value of a variable and then do something useful with
it).
Variables in PHP start with a dollar sign ("$"). Below I am setting
a variable, using it, then setting and using it again. The value that a
variable holds can be changed any time at all.
1. <?php
2. $utterance = "I love you!";
3. print ("When I'm happy, I want to
say $utterance");
4. print ("<p>");
5. $utterance = "I will smite you!";
6. print ("When I'm angry, I want to
say $utterance");
7. ?>
Here's what that page will look like:

In line two I have created a variable that I decided to name "utterance."
All variables start with "$", so my variable is
written "$utterance" in the code. Here's how that last code snippet
breaks down line by line.
Line 1 tells the browser: "Start PHP
code here".
Line 2 creates the variable $utterance and
also sets it, giving it the initial value of "I love you!".
Line 3 prints a phrase that draws on the
variable $utterance.
Line 4 creates a <p> tag in HTML to
put vertical space between the two utterances.
Line 5 RE-SETS the variable $utterance and
gives it the value "I will smite you!".
Line 6 prints a new phrase that draws on
the new meaning of the variable $utterance.
Line 7 tells Mr. Browser: PHP code ends
here.
See how the variable $utterance is used as a sort of container that can
hold different values? We just set and then called variables inside the
same script, but the power of PHP is that you can set a variable in one
place say from a form that a user fills out and then use that variable
later.
The syntax of setting a variable is to:
- define it with the = sign ($utterance = "I love you!";);
- use quotation marks if you're defining it with a string of letters ("I
love you!"; numbers don't require quotes);
- end each instruction with a semicolon.
Then you call it by refering to the variable name ($utterance in lines
3 and 6 notice no quotation marks there).
That word utterance is starting to sound awfully funny isn't it? Utterance
utterance utterance utterance utterance!
Naming Variables
You can name a variable anything you want so long as it follows these rules:
- it starts with a letter;
- it is made up of letters, numbers, and the underscore character (that's
the _ character, as in "$baby_names");
- it isn't used elsewhere (like "print").
Warning: Variable names are case-sensitive, so $baby_names and $Baby_names
are not the same.
You also should try to
make your names have some meaning so that you can still make sense of your
code next year.
In the examples so far, we have set variables as chunks of text, which are
known as "strings." Variables can also hold the values of numbers
and some other things as well (objects, arrays, booleans).
Final note: One thing that can be a little confusing when starting to use
PHP is the use of quotation marks inside PHP functions. Use single or double
quotes to set off strings (that is, chunks of text), as in:
print ("I am the CHICKEN MAN");
This will print the text I am the CHICKEN MAN. If you want to display quotation
marks as characters in the output of your PHP script, you must escape them
with the "\" character, which tells PHP not to use the next character
as part of the code. So to output the text "I am the CHICKEN MAN"
(with quotation marks showing in the result) the code would look like:
print (" \"I am the CHICKEN MAN\"" );
next page»