Variable Scope is an important issue in any programming language. Php is not an exceptional in this case. Each variable has an area in which it exists, known as its scope. When we declare a variable outside a function or an object, it may not be seen in the function or object. When we declare a variable inside a function or an object, it may not be seen outside the function or object. That feature is called variable scope. It is technically possible for a PHP script to have several variables called $a in existence at one point in time, however there's only one active $a at any one time.
Any variables not set inside a function or an object are considered global - that is, they are accessible from anywhere else in the script that is not inside another function or an object. Any variable used inside a function is by default limited to the local function scope. In PHP global variables must be declared global inside a function if they are going to be used in that function.
Consider the following code:
<?php
$a = 4;
function sendValue()
{
$a;
echo $a;
}
echo "Value of variable outside is ";
echo $a; echo ".<br />";
echo "Value of variable inside is";
sendValue();
?>
In PHP, any variable declared outside a function as $a above, is a global variable. In PHP any variable declared inside a function (see above), is a local variable. In the above code, the $a declared outside the function and the $a declared inside the function are entirely two different things.
Output of the above code:
Value of variable outside is 4.
Value of variable inside is
As we can see from the result, the two variables, though having the same name, but by the fact that one is outside the function and the other is inside, would hold different values. The one inside the function in this case, did not even acquire a value.
If we want the variable declared outside a function to hold the same value as the one inside the function, we have to re-declare the one inside the function, preceding it with the reserved word, global, as in the following code:
<?php
$a = 4;
function sendValue()
{
global $a;
echo $a;
}
echo "Value of variable outside is ";
echo $a; echo ".<br />";
echo "Value of variable inside is ";
sendValue();
?>
Output of the above code:
Value of variable outside is 4.
Value of variable inside is 4
From the result, the two values displayed are the same. This illustrates the use of the reserved word, global.
PHP has variables called super global variables, or simply, Superglobals. These are variables that can be seen outside and inside functions without the use of the reserved word, global. The programmer is not allowed to declare such variables. There are only some of them available and they are predefined. Examples of superglobals are the $_POST and $_GET variables. These are actually arrays, not variables.
Superglobals cannot be used as variable variables inside functions or class methods.
A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array.
Example
<?php
$a = 1;
$b = 2;
function Sum()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Sum();
echo $b;
?>
$a = 1;
$b = 2;
function Sum()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Sum();
echo $b;
?>
The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal.
No comments:
Post a Comment