PHP variables
Variables are a fundamental concept in many computer languages including PHP and knowing how to work with them is essential knowledge.
This lesson focuses on:
- What is a variable?
- Declaring variables
- Naming variables
- Printing variables
What is a variable?
A variable is a container which stores information in a computer's memory. The value of a variable can change all throughout a script.
Declaring variables
In PHP, a variable is declared with the $ symbol at the beginning. Declaring a variable without it is incorrect and will generate an error.
Syntax:
$varName
Example:
<?php $penColor; ?>
In the above example, a variable named penColor is declared. You can assign a value to a variable at the same time that it is declared. This process is known as initialization.
Example of initializing a variable:
<?php $penColor = "blue"; ?>
In the above example a variable named penColor is initialized with the value "blue"
Example of declaring a variable and then giving it a value:
<?php $penColor; $penColor = "blue"; ?>
In the above example a variable named penColor is declared. On the next line the variable penColor is assigned the value "blue"
NOTE: The $ symbol should always be used with variables, not just during declaration!
Although the variable name is declared the same way for numeric and text variables, the actual value is not. For text variables, the value is sorrounded by double quotes, while for numeric variables the value does not have double quotes around it.
Example:
<?php //declare a text variable and assign a value to it $penColor = "blue"; //declare a numeric variable and assign a value to it $age = 20; ?>
Sorrounding the value of a numeric variable with double quotes is a common error, this makes it a text variable. The value of a numeric variable should never be sorrounded by double quotes!
Naming variables
When naming variables, several rules should be followed:
-
Make sure that the variable name is descriptive.
If you do not give a variable a descriptive name, it will be hard to understand what the variable refers to. For example, if you wanted to create a variable which would hold a value signifying the amount of chairs in a room, which variable name would be more appropriate - numChairs or a? The better choice for the variable name would be numChairs because it is more descriptive.
-
Make sure the variable name is of appropriate length.
Make sure the variable name is long enough to be descriptive, but not too long.
-
Do not use spaces in variable names.
This is a rule that must be followed in PHP because PHP does not allow spaces in variable names. Using spaces in variable names will generate an error! If a variable name contains more than one word, you can use the underscore character ( _ ) or capitalization to separate the words.
Example:
<?php $a_really_long_variable_name; $numRightAnswers; ?>
-
Do not use special symbols in variable names such as !@#%^&*
As is the rules with spaces, special symbols are not allowed in variable names. Using special symbols in variable names will generate an error. There is however one special symbol that can be used in variable names, and that symbol is the undersocre ( _ ) symbol. Variable names can only contain letters, numbers and the underscore ( _ ) symbol.
-
Distinguish between uppercase and lowercase letters
PHP is a case sensitive language! This means that the variables valueOne, ValueOne, and VALUEONE are three separate variables.
-
When referring to existing variables, be careful about spelling
If you try to reference an existing variable and make a spelling mistake, PHP will not tell you about it and instead will create a new variable.
-
Be careful about the first character of a variables name
The first character of a variables name must be a letter or an underscore, it cannot be a number. If the first character of a variable name is not a letter or an underscore, an error will be generated!
Printing variables
Variables are printed by including the variable name in double quotes or without double quotes using the print or echo command.
Example:
<?php
$color = "blue";
$numChairs = 5;
print $color;
echo "<br />$numChairs";
?>
Output:
5
You can also print variables together with regular text. To do this, use the dot ( . ) symbol to join the text and variable values together.
Example:
<?php
$teamName = "The Penguins";
$teamState = "Connecticut";
$numWins = 12;
$numLosses = 2;
print "The name of the team is " . $teamName;
print "<br />The team is from " . $teamState;
print "<br />" . $teamName . " have won " . $numWins .
" games and have lost " . $numLosses . " games");
?>
Output:
The team is from Connecticut
The Penguins have won 12 games and have lost 2 games




