[ Team LiB ] |
![]() ![]() |
Variable ScopeA 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 Function1: <!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.
Similarly, a variable declared outside a function is not automatically available within it. Accessing Variables with the global StatementFrom 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 Default1: <!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.
Listing 6.8 Accessing Global Variables with the global Statement1: <!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.
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. |
[ Team LiB ] |
![]() ![]() |