Variables are containers used to hold information. Variables keep a value for future reference. 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 ($), 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.

How To Name PHP Variables

When you are naming a variable, keep the following rules in mind:

  1. Identifier: All variable names have a dollar sign ($) in front of them. This tells PHP that it is a variable name.

  2. Beginning of name: Variable names must begin with a letter or an underscore. They cannot begin with a number.

  3. Acceptable length: Variable names can be of any length.

  4. Acceptable characters: Variable names can include letters, numbers, and underscores only.

  5. Case sensitivity: Uppercase and lowercase letters are not the same. For example, $firstname and $Firstname are not the same variable.

PHP variables start with the $ sign followed by the variable name. A valid variable name starts with a letter or an underscore followed by any combination of letters, numbers or underscores. It is case sensitive.

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

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. You store information in variables with a single equal sign (=). The value of an unassigned variable will 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.

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:

  1. echo
  2. print
  3. print_r
  4. 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.