[ Team LiB ] Previous Section Next Section

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 of 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: <!DOCTYPE html PUBLIC
 2:    "-//W3C//DTD XHTML 1.0 Strict//EN"
 3:    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 4: <html>
 5: <head>
 6: <title>Listing 6.6 Local Variable Unavailable Outside a Function</title>
 7: </head>
 8: <body>
 9: <div>
10: <?php
11: function test() {
12:    $testvariable = "this is a test variable";
13: }
14: print "test variable: $testvariable<br/>";
15: ?>
16: </div>
17: </body>
18: </html>

You can see the output of the script in Listing 6.6 in Figure 6.2. The value of the variable $testvariable is not printed because no such variable exists outside the test() function. Note that the attempt in line 14 to access a nonexistent variable does not cause an error.

Figure 6.2. Attempting to reference a variable defined within a function.

graphics/06fig02.gif

graphics/bytheway_icon.gif

Attempting to access an undefined variable causes a NOTICE to be generated. A NOTICE is an error message associated with nonfatal error conditions. By default, NOTICE messages are not displayed. This behavior is dependent on the php.ini error_reporting directive, however. If error_reporting is set to include the E_NOTICE flag, such messages are displayed.

You can enable all error messages apart from NOTICE messages by altering the error_reporting flag in your php.ini file as follows:


error_reporting = E_ALL & -E_NOTICE


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

Accessing Variables with the global Statement

From within a function, by default you can't access a variable that has been defined elsewhere. If you attempt to use a variable of the same name, you will set or access a local variable only. 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: <!DOCTYPE html PUBLIC
 2:    "-//W3C//DTD XHTML 1.0 Strict//EN"
 3:    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 4: <html>
 5: <head>
 6: <title>Listing 6.7 No Default Access to Globals in Functions</title>
 7: </head>
 8: <body>
 9: <div>
10: <?php
11: $life = 42;
12: function meaningOfLife() {
13:   print "The meaning of life is $life<br />";
14: }
15: meaningOfLife();
16: ?>
17: </div>
18: </body>
19: </html>

You can see the output from the script in Listing 6.7 in Figure 6.3. As you might expect, the meaningOfLife() function has no access to the $life variable from line 11; $life is empty when the function attempts to print it. On the whole, this is a good thing. We're saved 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 might 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.

Figure 6.3. Attempting to print a global variable from within a function.

graphics/06fig03.gif

Listing 6.8 Accessing Global Variables with the global Statement
 1: <!DOCTYPE html PUBLIC
 2:   "-//W3C//DTD XHTML 1.0 Strict//EN"
 3:   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 4: <html>
 5: <head>
 6: <title>Listing 6.8 The global Statement</title>
 7: </head>
 8: <body>
 9: <div>
10: <?php
11: $life=42;
12:
13: function meaningOfLife() {
14:   global $life;
15:   print "The meaning of life is $life<br />";
16: }
17: meaningOfLife();
18: ?>
19: </div>
20: </body>
21: </html>

You can see the output from the script in Listing 6.8 in Figure 6.4. By placing global in front of the $life variable when we declare it in the meaning_of_life() function (line 14), we make it refer to the global $life variable declared outside the function (line 11).

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

graphics/06fig04.gif

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

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

You can declare more than one variable at a time with the global statement; you simply separate each of the variables you wish to access with commas:


global $var1, $var2, $var3;

In Hour 10, "Working with Forms," you will encounter the $GLOBALS superglobal array, which is a way of accessing global variables from anywhere in your script.

graphics/watchout_icon.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.


    [ Team LiB ] Previous Section Next Section