At some point, you will want to hold on to and manipulate data of varying sorts. This is done in PHP by using variables. Variables are a place to store data for later use. They are valid for the duration of the currently executing script.
PHP is different from many other languages, in that variables do not need to be declared before they are usedto declare one, you simply assign it a value. Variable names in PHP are represented by a dollar sign ($) followed by an identifier that begins with either a letter or underscore, which in turn can be followed by any number of underscores, numbers, or letters. Included in the set of letters permitted for variable names are some extended characters, such as accented Latin letters. Other extended characters, however, such as many Chinese characters seen in Japanese and Chinese alphabets, are not permitted.
<?php $varname = "moo"; // ok $var______Name = "oink"; // ok $__12345var = 12345; // ok $12345__var = 12345; // NOT ok - starts w/ number $école = "Rue St. Jacques"; // ok - é is an extended char $ = "car"; // NOT ok - has invalid chars ?>
In versions of PHP prior to version 4, variables would be declared at their first use instead of their first assignment, which often proved tricky when debugging code.
<?php $cieling = "roof"; // whoops misspelled it! echo "$ceiling"; // prints an empty string. ?>
Fortunately, PHP 5 prints a warning saying that, for instance, "$ceiling" has not been assigned a value.