Creating Variables


Variables in PHP are represented by a dollar sign followed by the name of the variable, which is case-sensitive. Variable names follow the same rules as other labels in PHP: a valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

Here's an example where we're creating two variables by assigning values to them:

 <?php     $variable = 5;     $another_variable = $variable; ?> 

By default, PHP variables are assigned by valuewhen you assign an expression to a variable, the entire value of the original expression is copied into the destination variable. This means, for example, that after assigning one variable's value to another, changing one of those variables will have no effect on the other. You can also assign values to variables by reference. This means that the new variable references the original variable, so changes to the new variable affect the original (note that this also means that no copying is performed).

To assign by reference, simply prepend an ampersand (&) to the variable being assigned. Here's an example:

 <?php     $variable = 5;     $new_variable = &$variable;     $variable = 6;    // Alters both $variable and $new_variable ?> 



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net