Dec
11
PHP Core - Variable Scope
December 11, 2007 |
There are four types of variable scope in PHP: local , global, static, and function parameters.
Local Scope:
In PHP, only functions can provide local scope (Unlike in other languages you can crreate a variable whose scope is a loop, conditional branch, or other type of block). Below is an example:
function update_counter ( ) {
$counter--;}$counter = 10;
update_counter( );
echo $counter;
By executing the PHP code above, the output you will get is 10 instead of 9, the variable $counter declname is inaccessible from outside greet( ).
ared in the function update_counter is local to that function. That is, it is visible only to code in the update_counter function (including nested function definitions); it is not accessible outside the function, so $counter still equals to 10.
Global scope:
In the above example, variable $counter=10 is declared outside the function update_counter, so it is global. It can be access from any part of the program, but by default, it is not available inside function update_counter or any other functions. To allow function update_counter to access the global variable $counter=10, you can use the global keyword inside the function to declare the variable within the function. So let’s rewrite the example above to make it work as we expected:
function update_counter ( ) {
global $counter;
$counter--;
}
$counter = 10;
update_counter( );
echo $counter;
By executing the code above, we should be able to get an output equals to 9. There is another way to do it, which is to use PHP’s $GLOBALS array instead of accessing the variable directly. It is a bit troublesome, but does the same job.
function update_counter ( ) {
$GLOBALS[counter]++;
}
$counter = 10;
update_counter( );
echo $counter;
Static Variable:
A static variable retains its value between calls to a function but is visible only within that function. You declare a variable static with the static keyword. It’s hard to explain, but a simple piece of code can explain itself:
function update_counter ( ) {
static $counter = 0;
$counter++;
echo "Static counter is now $counter\n";
}
$counter = 10;
update_counter( );
update_counter( );
echo "Global counter is $counter\n";
The returned result will look like:
Static counter is now 1
Static counter is now 2
Global counter is 10
The initial value of static $counter = 0, the first time it updates itself by adding 1 after 0, and equals to 1, so we first get Static counter is now 1. The second time, it updates itself again, and we get Static counter is now 2. The third time when we check the global variable, the output is: Global counter is 10, the value remains the same, so it is not affected by the static $counter
Function parameters
Function parameters are local, meaning that they are available only inside their functions.
function say ($something) {
echo "I say: $something\n";
}
say ("Hi Five!");
In the code above, $something is a function parameter of the function say, so $something is inaccessible from outside say ().
Similar Posts
- None Found


































