A variable is a holder for a type of data. So, based on its type, a variable can hold numbers, strings, booleans, objects, resources or it can be NULL. The variable name consists of letters, numbers and/or the underscore character (“_”), preceded by a dollar (“$”) sign. It cannot include spaces or non-alphanumeric characters. Here are some possible variable names: “$i”, “$a_very_long_variable_name”, “$6723”, “$TotalRESULT”. You should keep your variable names short and descriptive. A variable named “$f” is unlikely to mean much to you when you return to your code after a month or so. On the other hand, a variable named “$filename”, should make more sense.

The contents of a variable can be changed at any time, and so can its type. To declare a variable, you must include it in your script. You can declare a variable and assign it a value in the same statement. For example:

$number1 = 8; //assigns the value “8” to a variable called “$number1”

print $number1; //outputs the value that the variable “$number1” holds
This statement declares a variable using the assignment operator (“=”). The variable is called “$number1”, and it holds the value “8”. After the assignment, you can treat the variables as if they were values, so “print $number1” is equivalent to “print 8”, that is, if “$number” holds the value “8”. Remember that sometimes it is important to know the type of data a variable holds, so make sure that a variable contains an integer or a float before using it in a mathematical calculation, for example.

An interesting feature in PHP is dynamic variables. This means that a variable name can be stored into variable itself. Let’s consider the following:

$product = “computer”; //defines a variable called “$product”

$$product = “IBM”; //defines another variable called “$computer”

After parsing these two lines, PHP will a variable called “$computer” and assign it the value “IBM”. This little script is equivalent to:

$product = “computer”;

$computer = “IBM”;

This means that “$$product” is equivalent to “$computer”, because the variable “$product” holds the value “computer”.

Another nice feature is variable referencing. By default, variables are assigned by value. In other words, if you were to assign “$varA” to “$varB”, a copy of the value held in “$varA” would be then assigned to “$varB”. So if you change the value of “$varA”, it has absolutely no effect on “$varB”:

$varA = “one”; //assigns “one” to “$varA”

$varB = $varA; //assigns the value that “$varA” holds to “$varB”

$varA = “two”; //assigns “two” to “$varA”

print $varB; //outputs the value that “$varB” holds

This script outputs “one”, so the value of “$varB” is not changed in any way. On the other hand, you can assign a variable a reference to another variable by adding an ampersand (&) in front of the “$varA” when assign the value to “$varB”:

$varA = “one”; //assigns “one” to “$varA”

$varB = &$varA; //assigns a reference of “$varA” to “$varB”

$varA = “two”; //assigns “two” to “$varA”

print $varB; //outputs the value that “$varB” holds

This outputs “two”. Why, you ask? Because $varB holds a reference to $varA’s value, rather than a copy of its contents. So all changes made to $varA are seen when accessing $varB. In other words, both $varA and $varB now point to the same value.

While variables offer a flexible way of storing data, and you can freely change their values and the type of data they store at any time, you may sometimes want to work with a value that you don’t want to alter. This is where constants come in. You can use the PHP function define() to create a constant. After you’ve done this, its value cannot be changed. That value can only be a number or a string. You should know that by convention, the name of the constant should be in capitals. Unlike variables, constants don’t require a dollar symbol before their name:

define(“MAXIMUM_MARK”, 10); //defines a constant called “MAXIMUM_MARK”

define(“NAME”, “John Smith”); //defines a constant called “NAME”

print “Welcome ” . NAME; //outputs the value that the constant “NAME” holds

This outputs “Welcome John Smith”. Notice the concatenation operator (“.”), which appends the string “Welcome” and adds our constant.

PHP also provides a number of built-in constants for you. “__FILE__”, for example, returns the name of the file currently being read by the interpreter. “__LINE__” returns the line number of the file. These constants are useful for generating error messages. You can also find out which version of PHP is interpreting the script using the “PHP_VERSION” constant.