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.
When you are naming a variable, keep the following rules in mind:
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.
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
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.
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.
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.