PHP | Variable Scope

PHP Variable scope refers to the visibility of a variable inside the source code. In other words, variable scope denotes a boundary in which a variable is visible or accessed. Basically, it defines which part of the code uses that variable. To learn variable scope, you must be aware of function.

There are three types of variable scope in PHP namely:

  • Local Scope
  • Global Scope
  • Static Scope

Let us begin to learn the different scopes of variables in PHP step by step with the help of several examples.

Local variable

A PHP local variable is declared inside the function and has local scope. It means these variables can only be accessed inside the function. These variables cannot be accessed outside of the function because they have local scope.

Please keep in mind that a variable declared outside of the function with the same name is completely different from the variable that is declared inside the function.

Source Code

          <?php  
function local_scope(){  
$fruit = "grape";  //local variable  
echo "Local variable is defined inside the function & It's value is:  ". $fruit;  
 } 
local_scope(); 
echo "Local variable is defined inside the function & It's value is:  ". $fruit;  
//using  $fruit (local variable) outside the function will generate an error  
?>
        
Try it now

Global Scope

A variable declared outside of the function has a global scope and it will be accessed outside of the function but it can not be accessed inside the function.

Source Code

          <?php
$user = 'Ankita Maurya';
$job = 'Design & Developement';
function websiteName(){
echo $user;
echo $job."<br>";
//Normally global variable can not be access inside the function
}
websiteName(); 
//gloval variable can be access out side of the function
echo  $user." Working in the field of ".$job;

?>
        
Try it now

To access global variables inside the function, PHP provides two methods that are used to access global variables inside the function. These two methods are:

  • Using The global Keyword
  • Using The $GLOBALS Array

PHP global Keyword

To access the php global variables inside the function, use the global keyword before the variables. Although these variables can be directly accessed outside of the function without a global keyword.

Let us understand php global variable with the help of an example.

Source Code

          <?php
$actualCost = 5000;
// global scope
function totalCost() {
global $actualCost;  
//access global variable with global keyword
$transportCost = 2000; 
// local scope
$totalCost  = $actualCost+$transportCost; 
echo "Total cost of the product ".$totalCost;
} 
totalCost();
?>
        
Try it now

PHP $GLOBALS Array

PHP provides another way to access the global variables through the PHP global array. Keep in mind that the $GLOBALS array is used to save all the global variable. The array key contains the variable's name. The array value contains the variable value.

Source Code

          <?php
$username = 'Ankita Maurya';
$job = 'Designer & Developer';
function userInfo(){
echo $GLOBALS['username']."working in the field of ",$GLOBALS['job'];
}
userInfo(); // outputs HyvorDeveloper
?>
        
Try it now

PHP Static Scope

Please keep in mind that after the execution of the function all the variables are deleted but sometimes we have a requirement of the local variable for the next job. So to this, use the static keyword before the variable name inside the function and perform your next job.

In other words, static scope in PHP provides flexibility to store the variable even after the completion of the function execution for the next job.

Source Code

          <?php
function test() {
static $number = 0; 
// declare static variable
echo $number.'<br>'; 
$number = $number + 5; 
}
test(); // 0
test(); // 5
test(); // 10
?>
        
Try it now

Conclusion

php variable scopes refer to the availability of the variable inside the php code.There are mainly three types of variable scope in php.These are local scope, global scope, and static scope.

Local variables can be accessed inside the function only. It can not access outside of the function.

Although, Global variable can be accessed outside of the function as well inside of the function using global keyword.

Static variables exist only in a local function. It contains the variable value, even after the execution of the program.

Web Tutorials

PHP Variable Scope
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4