A common problem that the early programming languages had
was that there was no way of storing or reusing a piece of
data throughout a program. With the introduction of
variables, that problem was solved. Variables allow you to
store and reuse data throughout your program, and, today,
you'll find the use of variables in almost all programming
languages including PHP!
In PHP, you create variables by using a $ mark before
the name of your variable. Unlike other programming
languages, you do not have to declare a variable before
using it. All you have to do is give a variable name and
give that variable a value.
|
Note |
In the following
examples, I only provide the PHP code. You have to
provide the HTML and Body tags in the HTML to make
your page a real PHP page. I avoided using the HTML
opening and closing tags simply to focus only on
PHP! |
|
So let's get started. First, copy and paste the following
code into a PHP page:
- <?php
- $name
= Kirupa;
- print($name);
- ?>
When you preview the above page, you will see the value
you assigned for the variable name displayed. In our case,
the word "Kirupa" will appear.
Variables in PHP are not specific to any single data
type. You can easily assign your variable a string, a
number, or Boolean value without having to make any
modification to your variable. Test the following PHP code
in a new page:
- <?php
- $name
=
2004;
- print($name);
- print("<br>");
- $name
=
true;
- print($name);
- print("<br>");
- $name
=
Kirupa;
- print($name);
- ?>
If you tested out the above code, you will find that the
variable $name is versatile enough to display a number, the
value 1 for true, and the name in the form of a string.
No, variable scope isn't something that aliens would
use to examine a victim! Simply put, variable scope refers
to the locations PHP will recognize your variable. When you
use a variable, that variable will not always exist in all
areas of your computer program. I have provided a few
examples where a variable will exist or will not exist:
Inside a function:
- <?php
- function
kirupa()
{
- $who
=
kirupa;
- print("Inside
the function: $who.");
- }
- kirupa();
- print("<br>");
- print("Outside
the function: $who.");
- ?>
The variable $who exists within the function, but it does
not exist out of the function because the variable $who
exists - or is localized - only inside the function. PHP
does not recognize the function outside of the confines of
the function.
Multiple Locations in the same Page:
- <?php
- $name
= Kirupa;
- print("$name
says");
- ?>
- <p><font
size="4">Hello
everybody!
My
name
is
- <?php
- print("$name.");
- ?>
- </font></p>
As you can tell, the variable $name exists on the entire
PHP page, even if the PHP section the variable is first used
in is closed and re-opened later. Notice that I break the
PHP code by closing the tag to introduce a random bit of
HTML.
That's all for now in this introductory tutorial
regarding variables in PHP. There are other uses of
variables, but they will be covered at another time in
another tutorial.