Variable Scope

A variable declared within a function remains local to that function. In other words, it will not be available outside the function or within other functions. In larger projects, this can save you from accidentally overwriting the contents of a variable when you declare two variables with the same name in separate functions.

Listing 6.6 creates a variable within a function and then attempts to print it outside the function.

Listing 6.6 Variable Scope: A Variable Declared Within a Function Is Unavailable Outside the Function
  1: <html>
  2: <head>
  3: <title>Listing 6.6</title>
  4: </head>
  5: <body>
  6: <?php
  7: function test() {
  8:      $testvariable = "this is a test variable";
  9: }
 10: print "test variable: $testvariable<br>";
 11: ?>
 12: </body>
 13: </html>

Put these lines into a text file called scopetest.php, and place this file in your Web server document root. When you access this script through your Web browser, it should look like Figure 6.3.

Figure 6.3. Output of Listing 6.6.

graphics/06fig03.gif

The value of the variable $testvariable is not printed. This is because no such variable exists outside the test() function. Note that the attempt in line 10 to access a nonexistent variable does not cause an error.

Similarly, a variable declared outside a function will not automatically be available within it.

Accessing Variables with the global Statement

From within a function, it is not possible by default to access a variable that has been defined elsewhere. If you attempt to use a variable with the same name, you will only set or access a local variable. Let's put this to the test in Listing 6.7.

Listing 6.7 Variables Defined Outside Functions Are Inaccessible from Within a Function by Default
  1: <html>
  2: <head>
  3: <title>Listing 6.7</title>
  4: </head>
  5: <body>
  6: <?php
  7: $life = 42;
  8: function meaningOfLife() {
  9:      print "The meaning of life is $life<br>";
 10: }
 11: meaningOfLife();
 12: ?>
 13: </body>
 14: </html>

Put these lines into a text file called scopetest2.php, and place this file in your Web server document root. When you access this script through your Web browser, it should look like Figure 6.4.

Figure 6.4. Attempting to reference a variable from outside the scope of a function.

graphics/06fig04.gif

As you might expect, the meaningOfLife() function does not have access to the $life variable in line 7; $life is empty when the function attempts to print it. On the whole, this is a good thing; it saves us from potential clashes between identically named variables, and a function can always demand an argument if it needs information about the outside world. Occasionally, however, you may want to access an important global variable from within a function without passing it in as an argument. This is where the global statement comes into its own. Listing 6.8 uses global to restore order to the universe.

Listing 6.8 Accessing Global Variables with the global Statement
  1: <html>
  2: <head>
  3: <title>Listing 6.8</title>
  4: </head>
  5: <body>
  6: <?php
  7: $life=42;
  8: function meaningOfLife() {
  9:      global $life;
 10:      print "The meaning of life is $life<br>";
 11: }
 12: meaningOfLife();
 13: ?>
 14: </body>
 15: </html>

Put these lines into a text file called scopetest3.php, and place this file in your Web server document root. When you access this script through your Web browser, it should look like Figure 6.5.

Figure 6.5. Successfully accessing a global variable from within a function using the global keyword.

graphics/06fig05.gif

By placing global in front of the $life variable when we declare it in the meaningOfLife() function (line 9), we make it refer to the global $life variable declared outside the function (line 7).

You will need to use the global statement for every function that you want to access for a particular global variable.

Be careful, though. If we manipulate the contents of the variable within the function, $life will be changed for the script as a whole.

You can declare more than one variable at a time with the global statement by simply separating each of the variables you wish to access with commas.

global $var1, $var2, $var3;

graphics/clock.gif

Usually, an argument is a copy of whatever value is passed by the calling code; changing it in a function has no effect beyond the function block. Changing a global variable within a function, on the other hand, changes the original and not a copy. Use the global statement sparingly.




    Part III: Getting Involved with the Code