PHP Variables scope

the scope of a variable is the part of the script where the variable can be used.

php has three different variable scopes:

  • Variable global scope
  • Variable local scope
  • Variable static scope

PHP Variable global scope and local scope

a variable declared outside a function has a global scope and can only be accessed outside a function

a variable declared inside a function has a local scope and can only be accessed within that function

   <?php
   
                  $x = 10;
                  
                  $y = 20;     // global scope variables
                  
                           function   scope(){
                           
                           
                                                $x = 50;   // local scope variable
                                                
                                                $z = 40;     //z is local scope variable
                                                
                                                echo "x value = $x";
                                                
                                            //  echo "y value = $y";
                                                
                                                echo "z value = $z";
                                           }
                                             scope();
                                             
                                                echo "x value = $x";
                                                
                                                echo "y value = $y";
                                                
                                           //   echo "z value = $z";
                                                
            ?>                                           

when we output the values of the three variables inside the scope() method, it prints the value of $x and $z as it is the locally declared, but cannot print the value of $y since it is created outside the function.

then, when we output the values of the two variables outside the scope() function, it prints the value of $x and $y, but can't print the value of $z since it is a local variable and it is created inside the scope() function.


php global keyword

the global keyword is used to access a global variable from 'within a function'

   <?php
   
                      $a = 10;
                      
                      $b = 20;     // global scope
                      
                      $c = 0;
                      
 function scope(){
                               
                   global  $a, $b, $c ;
  
                    // global keyword use to access global scope variable within local scope
                                                  
                                                  
                    $b = $a + $b;
                                                        
                    $c = $b + $c;
       }
                                              
     scope();  // calling scope method
                                               
                  echo "a value = $a";
                                               
                  echo "b value = $b";
                                               
                  echo "c value = $c";
   ?>

a second way to access variables from the global scope is to use the special php defined $globals array.

syntax:

               $globals[index]

the index holds the name of the variable.

   <?php
   
                      $x = 10;
                      
                      $y = 20;     // global scope
                      
                      $z = 0;
                      
function scope(){
                                                                               
            $globals['y'] = $globals['x'] + $globals['y'];
                                                        
            $globals['x'] = $globals['y'] + $globals['z'];
   }
                                              
     scope();  // calling scope method
                                               
                  echo "x value = $x";
                                               
                  echo "y value = $y";
                                               
                  echo "z value = $z";
?>

PHP static keyword

a static variable exists only in a local function scope, but it does not lose its value when function execution completes.

note: static variable declarations are resolved in compile-time.

use the static keyword whitin method when you first declare the variable:

    <?php
    
              function  staticscope(){
                 
                                                 static   $xx = 0;
                             
                                                 echo "xx value = $xx";
                                             
                                                 $xx++;
                                              }
                                                      staticscope();
                                    
                                                      staticscope();
      
       ?>

in the staticscope() method, $x is initialized only in first call of function and every time the staticscope() function is called it will print the value of $x and increment it.