PHP - Scope of Variables
A variable can only be accessed in the area in which it is defined, which is called scope of the variable. In PHP, there are mainly three types of variable scopes:
- Local variable
- Global variable
- Static variable
Local Variable
If a variable is created inside a function, it is called local variable and has local scope. A local variable can only be used inside that function.
Example
In the example below, a local variable MyStr is created inside the function MyPrint(), which is accessible only inside that function.
<?php $MyStr = "Hello World!"; function MyPrint() { $MyStr = "Hello PHP"; echo "$MyStr \n"; } MyPrint(); echo "$MyStr \n"; ?>
The output of the above code will be:
Hello PHP Hello World!
Global Variable
A global variable can be used anywhere, inside the function and outside the function. If a variable is created outside a function, it will be global variable and has global scope. To access or create a global variable inside a function, it must be explicitly declared to be global in the function. This can be achieved by placing the keyword GLOBAL (or global) in front of the variable.
Example
In the example below, three global variables are created and used in different ways.
<?php //global variable created using GLOBAL keyword GLOBAL $var1; $var1 = 100; //global variable - created outside any function $var2 = 200; function MyPrint() { //to use global variable inside a function //it must be explicitly declared to be global //otherwise it will be treated as local variable GLOBAL $var1, $var2; //creating new global variable inside function GLOBAL $var3; $var3 = 300; //any changes done on global variable will //change its value outside the function also $var1 += 5; echo "\$var1 = $var1 \n"; echo "\$var2 = $var2 \n"; echo "\$var3 = $var3 \n"; } echo "Inside function: \n"; MyPrint(); echo "\nOutside function: \n"; echo "\$var1 = $var1 \n"; echo "\$var2 = $var2 \n"; echo "\$var3 = $var3 \n"; ?>
The output of the above code will be:
Inside function: $var1 = 105 $var2 = 200 $var3 = 300 Outside function: $var1 = 105 $var2 = 200 $var3 = 300
If a variable with same name is created inside the function (without using GLOBAL keyword), it will be a local variable and can be used inside the function only. Any operation performed on local variable will not change the global variable.
Example
Consider the example below:
<?php //global variable $var = 100; function MyPrint() { //local variable $var = 200; echo "$var \n"; } MyPrint(); echo "$var \n"; ?>
The output of the above code will be:
200 100
Using $GLOBALS instead of global
A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array. 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. Please note that $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal.
Example
In the example below, global variables are accessed inside a function using $GLOBALS.
<?php //creating global variables $var1 = 100; $var2 = 200; function MyPrint() { //global variables are accessed inside a function //using $GLOBALS $GLOBALS['var1'] = $GLOBALS['var1'] + 10; $GLOBALS['var2'] = $GLOBALS['var2'] + 20; } MyPrint(); echo "\$var1 = $var1 \n"; echo "\$var2 = $var2 \n"; ?>
The output of the above code will be:
$var1 = 110 $var2 = 220
Static Variable
A static variable exists only in a local function scope, but it does not lose its value when program execution leaves the function's scope.
A static variable can be created by placing the keyword STATIC (or static) in front of the variable name.
Example
Consider the example below, where a static variable count is created. It can only be accessed inside the function MyFunction() and preserves its values after each function call.
<?php function MyFunction() { //creating static variable STATIC $count = 0; $count++; echo "$count \n"; } MyFunction(); MyFunction(); MyFunction(); MyFunction(); MyFunction(); ?>
The output of the above code will be:
1 2 3 4 5