Control Statement
PHP Programs
PHP Functions
PHP Arrays
PHP Strings
PHP Math
PHP Form
PHP Include
State Management
PHP File
Upload Download
PHP Misc
PHP Mail
PHP MySQLi
PHP JSON
PHP OOPs Concepts
PHP Topics
PHP MCQ
Related Tutorials
Interview Questions
PHP Variable Scope
The scope of a variable is defined as its range in the program under which it can be accessed. In other words, "The scope of a variable is the portion of the program within which it is defined and can be accessed."
PHP has three types of variable scopes:
- Local variable
- Global variable
- Static variable
Local variable
The variables that are declared within a function are called local variables for that function. These local variables have their scope only in that particular function in which they are declared. This means that these variables cannot be accessed outside the function, as they have local scope.
A variable declaration outside the function with the same name is completely different from the variable declared inside the function. Let's understand the local variables with the help of an example:
File: local_variable1.php
{
$num = 45; //local variable
echo "Local variable declared inside the function is: ". $num;
}
local_var();
?>
Output:
File: local_variable2.php
{
$lang = "PHP";
echo "Web development language: " .$lang;
}
mytest();
//using $lang (local variable) outside the function will generate an error
echo $lang;
?>
Output:
Web development language: PHP Notice: Undefined variable: lang in D:\xampp\htdocs\program\p3.php on line 28
Global variable
The global variables are the variables that are declared outside the function. These variables can be accessed anywhere in the program. To access the global variable within a function, use the GLOBAL keyword before the variable. However, these variables can be directly accessed or used outside the function without any keyword. Therefore there is no need to use any keyword to access a global variable outside the function.
Let's understand the global variables with the help of an example:
Example:
File: global_variable1.php
function global_var()
{
global $name;
echo "Variable inside the function: ". $name;
echo "
";
}
global_var();
echo "Variable outside the function: ". $name;
?>
Output:
Variable outside the function: sanjeev
Example:
File: global_variable2.php
function global_var()
{
echo "Variable inside the function: ". $name;
echo "
";
}
global_var();
?>
Output:
Variable inside the function:
Using $GLOBALS instead of global
Another way to use the global variable inside the function is predefined $GLOBALS array.
Example:
File: global_variable3.php
$num2 = 13; //global variable
function global_var()
{
$sum = $GLOBALS['num1'] + $GLOBALS['num2'];
echo "Sum of global variables is: " .$sum;
}
global_var();
?>
Output:
If two variables, local and global, have the same name, then the local variable has higher priority than the global variable inside the function.
Example:
File: global_variable2.php
function mytest()
{
$x = 7;
echo "value of x: " .$x;
}
mytest();
?>
Output:
Static variable
It is a feature of PHP to delete the variable, once it completes its execution and memory is freed. Sometimes we need to store a variable even after completion of function execution. Therefore, another important feature of variable scoping is static variable. We use the static keyword before the variable to define a variable, and this variable is called as static variable.
Static variables exist only in a local function, but it does not free its memory after the program execution leaves the scope. Understand it with the help of an example:
Example:
File: static_variable.php
{
static $num1 = 3; //static variable
$num2 = 6; //Non-static variable
//increment in non-static variable
$num1++;
//increment in static variable
$num2++;
echo "Static: " .$num1 ."
";
echo "Non-static: " .$num2 ."
";
}
//first function call
static_var();
//second function call
static_var();
?>
Output:
Non-static: 7
Static: 5
Non-static: 7
You have to notice that $num1 regularly increments after each function call, whereas $num2 does not. This is why because $num1 is not a static variable, so it freed its memory after the execution of each function call.