Creating variables
Variables can hold either numbers or strings of characters. A variable can
exist or not exist and can hold information or not hold information; these are
two separate ideas. Even if a variable doesn’t currently contain any information,
it still can exist, just as a drawer exists even when it is empty. Of course,
if a variable contains information, it has to exist.
Storing information in a variable creates it.
To store information in a variable, you use a single equal sign (=). For example,
the following four PHP statements assign information to variables:
$age = 21;
$price = 20.52;
$temperature = -5;
$name = “Clark Kent”;
In these examples, notice that the numbers are not enclosed in quotes, but
the name, which is a string of characters, is. The quotes tell PHP that the
characters are a string, handled by PHP as a unit. Without the quotes, PHP
doesn’t know the characters are a string and won’t handle them correctly.
Whenever you put information into a variable that did not previously exist,
you create that variable. For example, suppose you use the following PHP
statements at the top of your script:
$color = “blue”;
$color = “red”;
If the first statement is the first time you mention the variable $color, this
statement creates the variable and sets it to “blue”. The next statement
changes the value of $color to “red”.
You can store the value of one variable in another variable, as shown in the
following statements:
$name1 = “Sally”;
$name2 = “Susan”;
$favorite_name = $name2;
After these statements are executed, the variable $favorite_name contains
the value “Susan”.
You can create a variable without storing any information in it. For example,
the following statement creates a variable:
$city = “”;
The variable now exists but does not contain any value