Variables are containers used in order to hold or store information. 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.
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 ($), and can be followed by 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’re 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.
Tip: 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.
Variables can hold numbers or strings of characters. You store information in variables with a single equal sign (=). In PHP, the equal sign (=) is used to assign what is on the right hand side to what is on the left hand side. Semi-colons are used to tell the interpreter where a statement ends, and where the next one begins.
$age = 12;
$price = 2.55;
$number = -2;
$name = 'Hello World';
The character string is enclosed in quotes, but the numbers are not.
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.