3.3. Variable ScopeUp to this point, if you don't use functions, any variable you create can be used anywhere in a page. With functions, this is no longer always true. Functions keep their own sets of variables that are distinct from those of the page and of other functions. The variables defined in a function, including its parameters, are not accessible outside the function, and, by default, variables defined outside a function are not accessible inside the function. The following example illustrates this: $a = 3; function foo( ) { $a += 2; } foo( ); echo $a; The variable $a inside the function foo( ) is a different variable than the variable $a outside the variable; even though foo( ) uses the add-and-assign operator, the value of the outer $a remains 3 throughout the life of the page. Inside the function, $a has the value 2. As we discussed in Chapter 2, the extent to which a variable can be seen in a program is called the scope of the variable. Variables created within a function are inside the scope of the function (i.e., have function-level scope). Variables created outside of functions and objects have global scope and exist anywhere outside of those functions and objects. A few variables provided by PHP have both function-level and global scope. At first glance, even an experienced programmer may think that in the previous example $a will be 5 by the time the echo statement is reached, so keep that in mind when choosing names for your variables. 3.3.1. Global VariablesIf you want a variable in the global scope to be accessible from within a function, you can use the global keyword. Its syntax is: global var1, var2, ... Changing the previous example to include a global keyword, we get: $a = 3; function foo( ) { global $a; $a += 2; } foo( ); echo $a; Instead of creating a new variable called $a with function-level scope, PHP uses the global $a within the function. Now, when the value of $a is displayed, it will be 5. You must include the global keyword in a function before any uses of the global variable or variables you want to access. Because they are declared before the body of the function, function parameters can never be global variables. Using global is equivalent to creating a reference to the variable in the $GLOBALS variable. That is, the following declarations: global $var; $var = &$GLOBALS['var']; both create a variable in the function's scope that is a reference to the same value as the variable $var in the global scope. 3.3.2. Static VariablesLike C, PHP supports declaring function variables static. A static variable is shared between all calls to the function and is initialized during a script's execution only the first time the function is called. To declare a function variable static, use the static keyword at the variable's first use. Typically, the first use of a static variable is to assign an initial value: static var [= value][, ... ]; In Example 3-4, the variable $count is incremented by one each time the function is called. Example 3-4. Static variable counterfunction counter( ) { static $count = 0; return $count++; } for ($i = 1; $i <= 5; $i++) { print counter( ); } When the function is called for the first time, the static variable $count is assigned a value of 0. The value is returned and $count is incremented. When the function ends, $count is not destroyed like a non-static variable, and its value remains the same until the next time counter( ) is called. The for loop displays the numbers from 0 to 4. Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|