Neticulis.com > Programming > Learning PHP
> Tutorial Lesson 2: Using Variables

Introduction:

A variable is just a container for information, it can be a number, a word, some sort of data, etc. In PHP a variable needs to start with a dollar sign ( $ ). This will let php know that what follows should be a variable. To put some sort of information into the variable, we use the equals sign ( = ), followed by the data we want to insert.

Step 1: Make 2 variables and display their contents
To start off, use the <?php ?> enclosure tag to embed your php into the page. Next, we want to make a new variable that will hold someones name. On the line directly after the <?php opening tag, type: $myName = "William"; This will declare a variable called $myName, with the contents "William". Next, we want to give this person an age, we do almost exactly the same thing, but this time, since the data is a number (not a word), we dont want to use quotes. So on the next line, type $myAge = 24; Now $myAge has the contents 24! Now we want to print this information to the screen, so on the next line, we will type: print "$myName is $myAge years old. <br />"; This should be self explanatory, it should print "William is 24 years old." to the screen, followed by a line break using HTML.
1
2
3
4
5
6
7
8
9
<html>
<body>
<?php
$myName = "William";
$myAge = 24;
print "$myName is $myAge years old.<br />";
?>

</body>
</html>


Step 2: Give William a last name and double his age.
How about we add a last name to William? But we dont want to have to make a whole new variable, so lets just add it onto the end of $myName, so on the line after your print command, we want to start a new line and type: $myName .= " Jackson"; So what did we just do? In this situation, we use .= The dot is perl symbol for concatenate, you can think of it as a tool to append some data on to the end of other data. Now $myName should have the contents "William Jackson". Now lets double the age, all we have to do is a little bit of math! Just start a new line and type: $myAge *= 2; This will multiply $myAge by 2, and put the result back into $myAge. Now lets print it all out again, so we can verify the changes! Just use the same print command on the next line.
1
2
3
4
5
6
7
8
9
10
11
12
<html>
<body>
<?php
$myName = "William";
$myAge = 24;
print "$myName is $myAge years old.<br />";
$myName .= " Jackson"; // The same as $myName = $myName . "Jackson";
$myAge *= 2;
// The same as $myAge = $myAge * 2;
print "$myName is $myAge years old.<br />";
?>
</body>
</html>

Step 3: Save and try it!
Just like in Lesson 1, save your new file as whatever you want, using the .php extension, and upload it to your web host, then use your browser to load the file and see if it works! You can see my version in action here.

Note:
There are many different types of data that can be inserted into a variable, such as an array, a string, an integer, a floating point number, and more. Most of these will be discussed later on, but they are all used in basically the same way.

Hopefully this tutorial has tought you the basics of using a variable in php. Now go on to Lesson 3!

Copyright 2005-2008 Neticulis.com