That's a very good question, just how variable are you?
To many beginners the subject of variables is usually pretty
scary, and often a reasonably difficult concept to grasp,
the reason for this however is usually because most modern
languages require some kind of indication as to what type of
data a variable will hold, this in turn often confuses
beginners because they don't know what type of data relates
to what kind of type.
The good thing about PHP is that this is not required,
instead PHP is smart enough to work out how to handle your
data automatically all you have to do is assign a value.
However, before we get to that point
..
What is a variable?
Put simply, a variable is an area of memory that has a
convenient name assigned to it, this area of memory can then
be used to store any data related to your script for further
use later in your script.
Under PHP, variables are indicated by prefixing the name with
a dollar sign. So for example to declare a variable that
might be used to hold a name you might use:
$name = "peter";
In the above case PHP assigns a string of data containing
the word "peter" to a memory address and then points the
identifier $name to it so that's it's easy to remember and
use.
Some other examples of variable usage are as follows:
$age = 21;
$date = "19/02/2009";
So what are variables good for?
Variables keep your data all in one place, and make changing
various aspects of your script very simple, consider the
following:
<html>
<head>
<title><?php print "My First Script"; ?></title>
</head>
<body>
<h1><?php print "My First Script"; ?></h1>
</body>
</html>
As you can see the same segment of PHP has been used twice
with exactly the same data. What if you needed to change
that data? You would need to make at least 2 changes to the
above script. Now imagine if that script was an extra 200
lines long and you had half of those lines with the phrase
"My First Script" in them.
Now lets take the previous example and use a variable in it:
<?php $phrase = "My First Script"; ?>
<html>
<head>
<title><?php print $phrase; ?></title>
</head>
<body>
<h1><?php print $phrase; ?></h1>
</body>
</html>
As you can see we've added a new line that sets the variable
named $phrase to the value of:
"My First Script"
The variable is then used in place of the previous 2 lines
where it's referenced simply by using it's name. If you try
changing the value of the variable, then re-run the script
in your browser you'll see that the text in the web-page
will change to match.
Variables can also take functions.
If you change the first line of the above script to be:
You'll see that your script then displays the current time
and date, just as with the previous parts script where we
fed the output directly to the print statement. The above
example feeds the output of the date to the contents of the
variable, which can then be reused.
Where does a variable belong?
One thing that very often confuses beginners to any language
is the subject of Scope.
"Scope" is how a variable is perceived by it's surrounding code depending on it's position in the source.
Put simply, it means that if your variable is boxed in (So
to speak), then code outside that box cannot see it's
contents, unless you explicitly tell it that it can. This
method has the advantage of allowing you to use the same
variable name in several different places without an issue,
if your careful. In practice however, it's normally not a
good idea.
So what is scope?
Consider the following PHP:
<?php
$variable1 = "one";
$variable2 = "two";
Function myfunction()
{
$variable3 = "three";
Print "in my function V1 is : " . $variable1 . "\n";
Print "in my function V2 is : " . $variable2 . "\n";
Print "in my function V3 is : " . $variable3 . "\n";
}
Print "Outside function : \n";
Print "in my function V1 is : " . $variable1 . "\n";
Print "in my function V2 is : " . $variable2 . "\n";
Print "in my function V3 is : " . $variable3 . "\n";
Myfunction();
Print "Outside function : \n";
Print "in my function V1 is : " . $variable1 . "\n";
Print "in my function V2 is : " . $variable2 . "\n";
Print "in my function V3 is : " . $variable3 . "\n";
?>
Don't worry if you don't understand it all just yet, all
that's important is the positioning of the $variable =
lines.
As you can see if you run it, in the first and last cases
$variable3 prints nothing, in-fact depending on how the
error reporting in your PHP is set up, you may even find
that an error is generated about missing variables.
What's happening here, is that $variable3 can ONLY be
seen within it's scope, which is the function. Outside of
that it cannot be seen, meanwhile $variable1 & 2 cannot be
seen inside the function as there scope only exists outside.
If we now add the following line just after the function
start, but before the $variable3 =
line :
Global $variable1,$variable2;
Now we re-run our script, you'll see that V1 & V2 are now
available to the function. This means in theory you could
set that variable, then work with it inside the function. In
practice however, that's not the best practice. The idea of
using functions is to pass in values, and return values
without using global variables, that however is a subject
for a different part of the series. For now, if your
variable doesn't hold the value you expect, where you expect
always check it's status, and see if it's global or not.
Some Variables are Special.
Some variables have a special purpose in PHP, that is they
are reserved to hold specific values and are created by PHP
itself, also built in function names cannot be used as
variable names.
The PHP manual has a full list of these variables, but some
of the more important ones are the array collections
$_POST , $_GET ,
$_REQUEST, $_SERVER and
$_ENV, We'll cover arrays in more detail at a
later date, but for now if you look at the phpinfo script
that we created in the second article in the series, you'll
see near the bottom that there is a list of the elements in
the $_SERVER and $_ENV variables.
To get the value of these variables simply use the variable
name followed by the item name quoted in square brackets,
eg:
print $_SERVER["SERVER_ADDR"];
The
$_POST,
$_GET and
$_REQUEST variables are populated by values
passed to your script. We'll look at $_POST in the article
on arrays, but for now if you try running your script using
http://localhost/myscript.php?myvar=hello&age=21,
you'll find that the variables
$_GET["myvar"]
and
$_GET["age"] will hold the values "
hello"
and
21 respectively.
Summary
In this episode of the ABC's of PHP we looked at the basics
of using variable data in your script, and how they can be
used, over the next three parts we'll look at the different
types of data in more detail starting in part 5 with strings
and text. Then in part 6 we'll cover maths and numbers.
In the mean time the variables page in the PHP manual is
worth a read, particularly the page on the predefined
variables, as there are a couple of warnings in there about
using globals, and the register-globals depreciated
functions.
Until next time
Happy scripting
Shawty