Powered by Blogger.

Introduction To PHP -Variables


Variables


There’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 (see Figure 3-2). Well,
that’s the same process as assigning a string value to a variable, like this:
$username = "Fred Smith";
Figure 3-2. You can think of variables as matchboxes containing items
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 the piece of paper out, and
read it. In PHP, doing so looks like this:
echo $username;
Or you can assign it to another variable (photocopy the paper and place the copy in
another matchbox), like this:
$current_user = $username;
50 | Chapter 3: Introduction to PHP