PHP Variable Scope

php variable scope

The part of a script where a variable is accessible after it is declared is its scope. Not always a variable could be accessed after it’s been declared in the remaining part of the script. The location of declaration determines scope of the variable.

Depending on the existence of function in a script, the scope can be divided into 2 parts-

  1. Local scope/ function scope: Local or function scope begins from the function declaration to the closing brace at the end of the function.
  2. Global scope: Other than the local scope, the remaining parts of the function are included in the global scope.

local scope and global scope in variable

As said before, depending on the location of declaration of a variable, its scope changes. Depending on the location of declaration, variable can be divided into the following types-

  1. Local variables.
  2. Global variables.
  3. Static variables.
  4. Function parameters.
  1. Local variables:

    • Variables declared inside the function are local variable to that function.
    • Its scope from the point where it is declared to the closing brace at the end of that function.
    • This variable dies when the function terminates.
    • This variable is not accessible outside the function.Example:
      <?php
          function site_name(){
              $name = “Schools of Web”;
           }
      
      ?>

      Explanation:
      In the example above, the variable $name is declared and assigned inside the function. It’s a local variable of that function.

    • Any variable with the same name outside the function can’t access the local variable.Example:
      <?php
          $name = “Learn PHP”;
          function site_name(){
              $name = “Schools of Web”;
          }
          echo $name;
      ?>

      Output:
      Learn PHP

      Explanation:
      Though the variable at line 2 and local variable (at line 4) have the same name $name, but php consider them different variables. So when we print the $name outside the function, it prints the value of the variable $name that is declared at line 2.

  2. Global variables:

    • Variables declared in a script outside the functions are called global variables.
    • Its scope exists from the point where it is declared to the end of the script except any function declared inside this area.
    • This variable dies when the script terminates.
    • This variable is not accessible inside any function.Example:
      <?php
          $name = “Learn PHP”;
          function site_name(){
              echo $name; // it will generate an error
          }
          site_name();
      ?>

      Output:
      Undefined variable name in c:\xampp\htdocs\scope.php on line 4

      Explanation:
      the variable $name is declared outside the function and it is a global variable. It can only be accessed outside the function. Therefore, when we try to print that variable inside the function, the script sends an error.

  3. Static variables:

    • After terminating a function the local variables inside it destroyed. Using the keyword static in front of a local variable, you can retain the value of that variable. It remembers the value from one function call to another in that script. This variable is called static variable.
    • Its scope exists from the point where it is declared to the closing brace at the end of that function.
    • This variable dies when the script terminates.
    • The value of the static variable is initialized only once when the function is called for the first time.Example:
      <?php
          function counter(){
              static $val=0;
              $val = $val+1;
              echo $val;
          }
          counter();
          echo "<br />";
          counter();
      ?>

      Output:
      1
      2

      Explanation:
      The variable $val inside the function in line 3 is a static variable. The function counter() is called at first at line 7. The variable $val is initialized at 0 at line 3. In the next line, the value is incremented by 1 and is stored in the same variable. In the next line (line 5), the variable is printed to 1. Before leaving the function, the variable $val preserve the value 1 in it. The function is called second time in line 9. This time the variable is not initialized again (at line 3). Rather the program control jump to the next line (line 4). Here 1 is added to the value of the variable $val which is also 1 (the static variable kept its last value 1) and insert 2 in the same variable. This 2 is printed in the next line.

    • You can’t use expression as value of static variable. You need to use value.Example:
      <?php
          function counter(){
              $a = 1;
              static $val=1;  // valid
              static $val=2-1;  // invalid
              static $val=$a+1;  // invalid
          }
      ?>
  4. Function parameters:

    • When a function that has arguments is called, it passes the values to the corresponding parameters of the receiver function. Thus, variables declared in the parameters.
    • Its scope exists from the point where it is declared to the closing brace at the end of that function.
    • This variable dies when the script terminates.Example:
      <?php
          function concate_name($f_name, $l_name){
              echo  $f_name . “ ” . $l_name;
          }
          concate_name(“John”, “Doe”);
      ?>

      Output:
      John Doe

      Explanation:

  5. Predefined variables or Superglobals

    • PHP has some predefined variables that are available in any scripts it runs.
    • Their scope is anywhere in the script whether it is inside or outside the function.Example:
      <?php
          $val = “Schools of Web”;
          function print_val(){
              echo $GLOBAL[“val”];
          }
          echo print_val();
      ?>

      Output:
      Schools of Web

      Explanation:
      The predefines variable $GLOBALS[] is used to access variables from global access. Here the variable $val a global variable.

  6. Constants:

    • Though constant is not variable, but it has a scope too.
    • Their scope is anywhere in the script whether it is inside or outside the function.Example:
      <?php
          define("SITE_NAME","Schools of Web");
          function print_val(){
              echo SITE_NAME;
          }
          echo print_val();
      ?>

      Output:
      Schools of Web

      Explanation:
      After declaring the constant outside the function can be accessed anywhere in the script even inside the function. In the previous example, the constant is used inside the function.

How to convert a global variable to local variable?

  • Using the keyword global in front of a local variable, you can set the scope of that variable as global.
  • That means, any variable with the same name declared before the function can be accessible within the function.Example:
    <?php
        $val = 3;
        function show_val(){
            global $val;
            echo $val;
        }
        show_val();
    ?>

    Output:
    3

    Explanation:
    By declaring the variable $val as global inside the function at line 4, you can access the value(which is 4) of the variable $val declared at line 2 inside that function. The value 3 is printed at line 5 when the function is called.

  • Also, any changes made in that variable inside the function will also reflect in that variable after the function.Example:
    <?php
        $val = 3;
        function show_val(){
            global $val;
            $val = 5;
        }
        show_val();
        echo $val;
    ?>

    Output:
    5

    Explanation:
    At line 2, the value of the variable $val is 3. Declaring the $val as global at line 4, you can use that variable that same variable that is declared at line 2. When the function show_val() is called at line 7, the value of that variable is changed to 5 at line 5. This value is printed at line 8.

‹‹ PHP Functions : Previous Lesson Next Lesson: PHP Forms – Part1 ››


Pin It

↑ Back to Top