Web Statistics

Variable Scope

Where did my variable(s) go?

Getting to know Variable Scope

We are wrapping up our discussion about variables by walking through the different scopes and how they effect your programming. To begin it's best to first understand some of the terminology that will be using.

Scope can be defined as the range of availability a variable has to the program in which it is declared. So far we have been defining variables and doing so in a local scope. What is meant by this is that the availability of the variable is only available to the localized position of the variable.

Here we will cover the other scopes a variable may be defined as.

Variables with Global Scope

The purpose of a global variable is so that it can be accessed in any part of the program. This includes it's availability inside classes and functions. However, a global variable must be explicitly declared to be global in the function in which it is to be modified. To initialize a global variable you will need to declare the variable by placing the keyword GLOBAL in front of the variable. Have a look at the following example.

				$my_var = 20;

function foo() {
    GLOBAL $my_var;
    print "my_var is equal to $my_var";
}

foo();
			

The example above shows how to increase the local variable $my_var to a global scope so that it may be accessed inside the function foo. The result of the script is:

				my_var is equal to 20
			

If we were to remove the line containing the GLOBAL keyword the script would throw an "ErrorException in line xx: Undefined variable: my_var".

Variables with Superglobal Scope

According to www.PHP.net, "Superglobals are built-in variables that are always available in all scopes." The basic concept here is that a superglobal can be accessed at any point in the program. There variables are:

  • $GLOBALS - This variable stores all the variables in the global scope as an associative array.
  • $_SERVER - This is an associative array that includes server and runtime environment information such as: server IP, client IP, http referer, request URI, etc...
  • $_GET - An associative array of the HTTP GET payload.
  • $_POST - An associative array of the HTTP POST payload.
  • $_FILES - An associative array of items uploaded to the current script via the HTTP POST method.
  • $_COOKIE - An associative array of variables passed to the current script via HTTP Cookies.
  • $_SESSION - An associative array containing session variables available to the current script.
  • $_REQUEST - An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.
  • $_COOKIE - An associative array of variables passed to the current script via HTTP Cookies.

Static Variables

A static variable is one that will not loose its value when the function exits. It will also retain that value should the function be executed again. Non-static variable will naturally be destroyed upon exiting the function, and will have to be recreated if the function is executed again.

Take the following example of how a static variable is declared and used.

				
function foo() {
   STATIC $integer = (int) 0;
   $integer++;
   print $integer;
   print "<br />";
}

foo();
foo();
foo();
				
			

The result of the script above would produce:

				
1
2
3
				
			

Up Next

Constants!!!