PHP Manual
Prev | Chapter 7. Variables | Next

# Variable scope

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example:

      1
      2 $a = 1;
      3 include "b.inc";
      4

Here the $a variable will be available within the included b.inc script. However, within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example:

      1
      2 $a = 1; /* global scope */
      3
      4 Function Test () {
      5     echo $a; /* reference to local scope variable */
      6 }
      7
      8 Test ();
      9

This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in that function. An example:

      1
      2 $a = 1;
      3 $b = 2;
      4
      5 Function Sum () {
      6     global $a, $b;
      7
      8     $b = $a + $b;
      9 }
     10
     11 Sum ();
     12 echo $b;
     13

The above script will output "3". By declaring $a and $b global within the function, all references to either variable will refer to the global version. There is no limit to the number of global variables that can be manipulated by a function.

A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array. The previous example can be rewritten as:

      1
      2 $a = 1;
      3 $b = 2;
      4
      5 Function Sum () {
      6     $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"];
      7 }
      8
      9 Sum ();
     10 echo $b;
     11

The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element.

Another important feature of variable scoping is the static variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. Consider the following example:

      1
      2 Function Test () {
      3     $a = 0;
      4     echo $a;
      5     $a++;
      6 }
      7

This function is quite useless since every time it is called it sets $a to 0 and prints "0". The $a++ which increments the variable serves no purpose since as soon as the function exits the $a variable disappears. To make a useful counting function which will not lose track of the current count, the $a variable is declared static:

      1
      2 Function Test () {
      3     static $a = 0;
      4     echo $a;
      5     $a++;
      6 }
      7

Now, every time the Test() function is called it will print the value of $a and increment it.

Static variables also provide one way to deal with recursive functions. A recursive function is one which calls itself. Care must be taken when writing a recursive function because it is possible to make it recurse indefinitely. You must make sure you have an adequate way of terminating the recursion. The following simple function recursively counts to 10, using the static variable $count to know when to stop:

      1
      2 Function Test () {
      3     static $count = 0;
      4
      5     $count++;
      6     echo $count;
      7     if ($count < 10) {
      8         Test ();
      9     }
     10     $count--;
     11 }
     12

Prev | Home | Next
Predefined variables | Up | Variable variables