Saturday, December 8, 2007

Variables

Variables

A variable in PHP, like in most other languages, is simply a container for some data. You can give your variables names, put data into them, and then refer back to them at a later point in your program to access the stored data.

PHP identifies variables using the dollar sign ($). The name for a variable will always begin with this character whenever you refer to it. After the dollar sign, your variable name can consist of any number of alphanumeric characters and underscores, although the first character cannot be a number or an underscore. So, the following are all valid variable names:

    $a
$a2
$my_name
$height_in_metres_above_sea_level

The other thing to remember is that PHP variable names (unlike keywords) are case sensitive, so all of the following refer to different variables:

    $my_name
$MY_NAME
$My_Name

PHP doesn't require you to declare variables before you use them, or to tell it what kind of data you plan to store in the variable. The same variable can, over the course of a program, store many different data types. A variable is created as soon as you assign it, and then exists for as long as the program is executing. In the case of a web page, that means it exists until the request has been completed.


Note

The exceptions to this are functions, which have their own variable scope.

Assignment

To assign a value to a variable, you use an assignment statement, in this form:

    $variable_name = expression;

We'll look at expressions in more detail in a moment. For now, let's assume that the expression is either a literal, or another variable. That means that the following will all work:

       $a = "Hello";
$b = 123;
$c = $a;
?>

Now that we've got a variable, we can use it in the same way as we used literals before:

       $a = "Hello";
echo($a);
?>

As we said, when we enclose a string in double quotes, PHP looks through it for variable names and substitutes in values. Now we can see how that might work:

       $a = "Hello";
echo("$a World!");
?>

Now, even though the variable reference is inside a string literal, it is still evaluated, and the output is:

    Hello World!

Note that it is evaluated at the time that the literal is interpreted. In the following example:

       $a = "Hello";
$b = "$a World!";
$a = "Goodbye";
echo($b);
?>

The output is still Hello World! because the variable substitution takes place at the point that the literal is interpreted into a string and stored in the variable $b. The subsequent change to $a doesn't affect $b.

Reference

PHP also provides another way of referencing variables. A variable reference of the form:

    ${expression}

refers to the variable whose name is the result of evaluating the expression. So, if the expression is a string literal, like this:

    ${"my_name"}

the variable referred to is $my_name. If the variable $a contained the string "name", then:

    ${"my_$a"}

would also refer to the variable $my_name – the string is evaluated, as before, to the string "my_name", and this is used to refer to the variable. Now, if the variable $a were to contain "my_name", then:

    ${$a}

would also refer to $my_name. However, PHP also provides an even shorter syntax for this special case. If the name of the variable is simply contained in another variable, like this, then you can use this notation:

    $$a

to refer to the variable $my_name.

These substitutions will work anywhere where a variable reference is required, including on the left hand side of assignment statements, and within double-quoted strings.

No comments: