Variables are a key element in programming languages and PHP is no exception. In this tutorial I’ll explain what they are for and some examples of how to use them.
Simply put, variables contain dynamic information. Variables are useful when the data might change, or vary, which happens very often in Web applications. If the information is the same every time, then you can just type the data ahead of time with no need for a variable. For example, you may store a user’s name in a variable, or the total number of items in their shopping cart, or their favorite color. All of this information could change.
Variables have rules that must be followed:
1. All variable names start with a dollar sign, $. When PHP sees this, it knows it’s a variable.
2. Names can use letters, numbers, underscores and dashes only. However, they must begin with a letter or underscore.
3. Variables are case sensitive and unique. $firstName and $FirstName and $first_Name are three different variables.
Variable names should be descriptive of what they are being used for. So if you’re storing a customer’s name, the variable might be $FullName.
Let’s create some variables.
1. If WampServer is not running, go ahead and start it. Then open your code editor, create a new file called variables.php and save it to your “introduction” directory under c:\wamp\www.
2. Type in the code below (with or without the comments — your choice):
<html> <head> <title>Variables</title> </head> <body> <?php $age = 20; // Set the $age variable to 20. echo $age; // Output the $age variable to the browser. ?> </body> </html>
3. Save the file. In your Web browser, go to: http://localhost/introduction/variables.php
You should see the number 20. So we created a new variable called $age and set the initial value to 20. Let’s modify it a little.
4. Type in the code below, save, and reload the page in your browser:
<html> <head> <title>Variables</title> </head> <body> <?php $age = 20; // Set the $age variable to 20. echo $age+10; // Output the $age variable to the browser. ?> </body> </html>
You should now see the number 30. PHP has the ability to process math, just like a calculator. In the example above, we took our variable $age, which was equal to 20, and asked PHP to add 10 to it. This worked because our variable happened to be a number. Let’s try another variable.
5. Type in the code below, save, and reload the page in your browser:
<html> <head> <title>Variables</title> </head> <body> <?php $age = 20; // Set the $age variable to 20. echo $age; // Output the $age variable to the browser. echo '<br />'; // Inserts the HTML break to output a new line. $name = 'John Smith '; echo $name . 'is ' . $age . ' years old.'; ?> </body> </html>
Your browser should now display “John Smith is 20 years old.” In the code above, we created two variables and output them together using connotation.
6. Variables can be set multiple times in one file. Type in the code below, save, and reload the page in your browser:
<html> <head> <title>Variables</title> </head> <body> <?php $age = 20; // Set the $age variable to 20. echo $age; // Output the $age variable to the browser. echo '<br />'; // Inserts the HTML break to output a new line. $age = 15; // Change the $age variable to 15. $name = 'John Smith '; echo $name . 'is ' . $age . ' years old.'; ?> </body> </html>
Notice how we changed the $age variable. Now the browser will tell you John Smith is 15 years old.