PHP Variables
Variables are used for storing data, such as numbers or strings, so that they can be used multiple times in a script with a meaningful name (an identifier).
They are called variables because the value they refer to can change each time a program is executed. For example, a variable might refer to the current date or time, and a program might have logic to do something special based on the value of this variable.
For example, you might name a variable $age and store the number 12 in it. Information stored in a variable can be used later in the script. This value can change if you want it to, that is why they are called variables.
How to Create Variables
You create a PHP variable by giving it a name and assigning it a value. For example, to create a variable called $age and
assign it a value of 12:
$age = 12;
PHP is a loosely typed language, which means you do not have to tell the computer what type of information you are going to keep in a variable. In strictly typed languages (like C++), you have to specify if the variable is going to be an integer, string, float or something else.
Variables in PHP must begin with a dollar sign ($), followed by an identifier, which is the name of the variable. The name of the variable can start with an underscore (_) or letter (a through z, both upper and lower case). Variables cannot start with a number, but may contain numbers after an underscore or at least one letter.
Assigning a value to a variable is done using assignment operator. The variable name goes on the left of the equal sign and its value on the right. Since setting a variable’s value is a type of statement, it ends with a semicolon. The code to the right of the assignment operator is an expression. An expression is something that yields a single value or can be evaluated into a single value.
How to Name PHP Variables
When you are naming a variable, keep the following rules in mind:
- Identifier: All variable names have a dollar sign ($) in front of them.
- Beginning of Name: Variable names must begin with a letter or an underscore. They cannot begin with a number.
- Acceptable Length: Variable names can be of any length.
- Acceptable Characters: Variable names can include letters (A-z), numbers (0-9), and underscores (_) only.
- Case Sensitivity: Uppercase and lowercase letters are not the same. For example, $name and $Name are not the same variable.
When you name variables, use names that make it clear what information is in the variable. Using variable names like $var1, $var2, $A, or $B doesn't contribute to the clarity of the script.
How to Assign Values to Variables
Assigning a value to a variable means to give it a value, and it is done with the equals sign or assignment operator (=). The value of an unassigned variable can be null, that is, nothing.
For example:
$age = 12;
$price = 2.55;
$number = -2;
$name = 'Hello World';
The character string is enclosed in quotes, but the numbers are not.
Using Variables
Once you have created a variable, you can use it by referencing the variable’s name. For example, the value of the variable can be printed to the web page by using echo followed by the variable's name.
echo $age; // 12
Single or Double Quotes
PHP lets you use both single and double quotes when defining strings.
Single quoted strings are treated by the interpreter as plain text, meaning the output to the screen will be exactly what is in quotes.
Double quoted strings are examined by the interpreter for anything that can be processed by PHP, which means items like special characters and variables are replaced with what they represent before the output is sent to the screen.
Escaping
Escape characters are symbols with special, secondary meaning. PHP’s escaping character is backslash (\). In PHP, escape characters are commonly used in double-quoted strings, so you can include special characters.
- \" Print the double quote, not use it as a string opening or closing marker
- \’ Print the single quote, not use it as a string opening or closing marker
- \n Print a new line character (for text or output files, not on the screen)
- \t Print a tab character
- \r Print a carriage return (for text or output files, not on the screen)
- \$ Print the next character as a dollar sign, not as part of a variable
- \\ Print the next character as a backslash, not an escape character
Predefined Variables
PHP maintains some variables of its own, called predefined variables or global variables. These start with underscores, and hold certain types of values.
Several of these variables ($_GET, $_POST and $_FILES) hold items a user has typed or submitted using forms. $_COOKIE and $_SESSION are used to hold information throughout a user’s visit, and $_ENV holds information about the server.
Variable Variables
PHP allows to use dynamic variable names, called variable variables. You can name a variable with the value stored in another variable. One variable contains the name of another variable. For example, suppose you want to construct a variable named $city with the value New York. You can use the following statement:
$name_of_variable = "city";
This statement creates a variable that contains the name that you want to give to a variable. Then, use the following statement:
$$name_of_variable = "New York";
The extra dollar sign ($) character at the beginning of the variable name indicates a variable variable.
How to Display Variable Value
You can display the value in a variable by using any of the following statements:
- echo
- print_r
- var_dump
You can display the value in a variable on a web page with an echo or print statement. For example, if you set the $age variable to 12 and then use the following PHP echo statement
echo $age;
PHP provides a function named print_r for looking at the value in a variable. You can write the following statements to display a variable value:
$weekday = "Monday";
print_r($weekday);
PHP provides a function named var_dump that you can use to display a variable value and its data type.
You can write the following statements to display a variable value:
$weekday = "Monday";
var_dump($weekday);
You can use var_dump for troubleshooting PHP. Its use is essential for that purpose.