Variables are used for storing data, such as numbers or strings, so that they can be used multiple times in a script. A variable has a name, and information is stored in the 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.
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 would have to specify if the variable was going to be an integer, string, float, or other option supported by the language in use.
Variables in PHP must start 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.
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. This tells PHP that it is a variable name.
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, numbers, and underscores only.
Case sensitivity: Uppercase and lowercase letters are not the same. For example, $firstname and $Firstname 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.
A common naming convention for variables is to have each word initially capitalized, except for the first one.
How to Assign Values to Variables
Variables can hold numbers or strings of characters. Assigning a value to a variable means to give it a value, and it is done with the equals sign or assignment operator (=). You store information in variables with a single equal sign (=). The value of an unassigned variable can be null, that is, nothing.
$age = 12;
$price = 2.55;
$number = -2;
$name = 'Hello World';
The character string is enclosed in quotes, but the numbers are not.
Once a variable has been defined, it can be used 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.
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
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.
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:
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
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";
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";
You can use var_dump for troubleshooting PHP. Its use is essential for that purpose.