Searching...
Friday 14 June 2013

PHP Basics - Part 5 :: Variables Part 2 :: Scope 1


Variable Scope

However you declare your variables (by value or by reference), you can declare them anywhere in a PHP script. The location of the declaration greatly influences the realm in which a variable can be accessed, however. This accessibility domain is known as its scope.

PHP variables can be one of four scope types:

Local Variables

A variable declared in a function is considered local. That is, it can be referenced only in that function. Any assignment outside of that function will be considered to be an entirely different variable from the one contained in the function.

Note:- When you exit the function in which a local variable has been declared, that variable and its corresponding value are destroyed.

Local variables are helpful because they eliminate the possibility of unexpected side effects that can result from globally accessible variables that are modified, intentionally or not. Consider this listing:

$x = 4;
function assignx () {
$x = 0;
printf("\$x inside function is %d <br />", $x);
}
assignx();
printf("\$x outside of function is %d <br />", $x);

Executing this listing results in the following:

$x inside function is 0
$x outside of function is 4

As you can see, two different values for $x are output. This is because the $x located inside the assignx() function is local. Modifying the value of the local $x has no bearing on any values located outside of the function. On the same note, modifying the $x located outside of the function has no bearing on any variables contained in assignx().
Note:- In the above example, a backslash precedes the dollar sign because I want the dollar sign to be treated as a normal string character rather than prompt PHP to treat $x as a variable. A backslash used in this manner is known as an escape character.

Global Variables

In contrast to local variables, a global variable can be accessed in any part of the program. To modify a global variable, however, it must be explicitly declared to be global in the function in which it is to be modified. This is accomplished, conveniently enough, by placing the keyword global in front of the variable that should be recognized as global. Placing this keyword in front of an already existing variable tells PHP to use the variable having that name. Consider an example:

$somevar = 15;
function addit() {
global $somevar;
$somevar++;
echo "Somevar is $somevar";
}
addit();

The displayed value of $somevar would be 16. However, if you were to omit this line, global $somevar; then the variable $somevar would be assigned the value 1 because $somevar would then be considered local within the addit() function. This local declaration would be implicitly set to 0 and then incremented by 1 to display the value 1.

An alternative method for declaring a variable to be global is to use PHP’s $GLOBALS array. Reconsidering the preceding example, you can use this array to declare the variable $somevar to be global:

$somevar = 15;
function addit() {
$GLOBALS["somevar"]++;
}
addit();
echo "Somevar is ".$GLOBALS["somevar"];

This returns the following:

Somevar is 16

Regardless of the method you choose to convert a variable to global scope, be aware that the global scope has long been a cause of grief among programmers due to unexpected results that may arise from its careless use. Therefore, although global variables can be useful, be carefull when using them.

0 comments:

Post a Comment

 
Back to top!