PHP variables scope

PHP variables scope

The scope of a variable is defined as its range in the program under which it can be accessed. 

PHP has three types of variable scopes:
  • Local variable scope
  • Global variable scope
  • Static variable scope

Local variableThe 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.

E.g. By using variable inside the function

<?php  
    function var_local()  
    {  
        $val = 10;    //local variable  
        echo "Local variable declared inside the function is: ". $val;  
    }  
    var_local();  
?>  

Output:

Local variable declared inside the function is: 10

E.g. By using variable outside the function

<?php  
    function testvar()  
    {  
        $val = 10;       
    }  
    testvar();  
    //using $val (local variable) outside the function will generate an error  
    echo $val;  
?>  

Output:

Notice: Undefined variable: val in c:\\wamp\www\lvar.php on line 21

Global variableGlobal variables are the variables that are declared outside the function and these variables can be accessed anywhere outside the function or class in the program. 

E.g.

<?php  
    $msg = "Hello PHP"; //Global Variable  
    function global_var()  
    {  
       echo $msg; //display an error or nothing
    }  
    global_var();  
    echo $msg;  //display an output
?>  

Output:

Notice: Undefined variable: msg in c:\\wamp\www\index.php on line 17
Hello PHP      

Global Variable inside the function

To access the global variable inside the function, we can use the GLOBAL keyword or $GLOBALS array before the variable. 

Example: Using GLOBAL keyword

<?php  
    $msg = "Hello PHP"; //Global Variable  
    function global_var()  
    {  
      GLOBAL $msg; 
      echo $msg;
    }  
    global_var();  
?>  

Output:

Hello PHP

Example: Using $GLOBALS array

<?php  
    $msg = "Hello PHP"; //Global Variable  
    function global_var()  
    {  
      echo $GLOBALS['msg']; 
    }  
    global_var();  
?>  

Output:

Hello PHP

Static variable- PHP 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. So to fullfill this purpose we can use static variable. 

We use the Static keyword before the variable to define a Static variable.

E.g.

<?php  
    function static_var()  
    {  
        Static $num1 = 2; //static variable  
        $num2 = 4; //Non-static variable    
        $num1++;  //increment in static variable
        $num2++;  //increment in non-static variable
        echo "Static variable output: " .$num1 ."</br>";  
        echo "Non-static variable output: " .$num2 ."</br>";  
    }  
    static_var();  //function first call  
    static_var();  //function second call  
?>  

Output:
Static variable output: 3
Non-static variable output: 5
Static variable output: 4
Non-static variable output: 5