Sunday, September 30, 2007

PHP learning--Part 5

Variables


+ 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. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

Note: For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 127 through 255 (0x7f-0xff).

Varible assignment
+ Assigned by value:  That is to say, when you assign an expression to a variable, the entire value of the original expression is copied into the destination variable. This means, for instance, that after assigning one variable's value to another, changing one of those variables will have no effect on the other
+ Assigned by reference: This means that the new variable simply references (in other words, "becomes an alias for" or "points to") the original variable.Changes to the new variable affect the original, and vice versa.

To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable)
Example:

$bar = &$foo;

Note:

PHP.Net: Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset() language construct can be used to detect if a variable has been already initialized.

Comment: The good behaviour is always initialize a varible before using it. It is always recommended on every programming language not only for PHP.

Variable scope

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well.

Example:

<?php
$a
= 1;
include
'b.inc';
?>

Varible $a will be available within the included b.inc script.

+ Any variable used inside a function is by default limited to the local function scope. For example:

<?php
$a
= 1; /* global scope */

function Test()
{
    echo
$a; /* reference to local scope variable */
}

Test();
?>

This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope


To used a global variable in the function we used the global keyword

Example:

<?php
$a
= 1;
$b = 2;

function
Sum()
{
    global
$a, $b;

   
$b = $a + $b;
}

Sum();
echo
$b;
?>

Variable a, b in function is declared as global variable, all the references to either variable will refer to the global function.

The second way to access varibles from global scope is to use the special PHP-defined $GLOBALS Array. This $GLOBALS variable is the superglobal and can be used in any scope.

Static variable:
The static varible is available in local function scope but it is does notlose its value when program excution leaves this scope. Consider the following example:

<?php
function Test()
{
    static
$a = 0; //just for defined static variable.
    echo
$a;
   
$a++;
}
?>

When we call Test the first times, it output: 0.
The second times, it output 1;

References with global and static variables

This can lead to unexpected behaviour which the following example addresses. So do not use it.

Next part will be in next day.
I want thank to php.net with the manual. This part what i learn from their manual.

No comments:

Google