I l @ ve RuBoard |
Variables and FunctionsI did not introduce the concept of variable scope in Chapter 2, Variables , because without an understanding of functions, scope makes little sense. Now that you have a reasonable familiarity with functions, I'll revisit the topic of variables and discuss in some detail how exactly variables and functions work together. Variable scope and the global statementAs you saw in the second section of this chapter, Creating and Calling Functions that Take Arguments , you can send variables to a function by passing them as arguments. However, you can also call a variable from within a function using the global statement. This is possible because of variable scope. The scope of a variable is essentially the realm in which it exists. By default, the variables you write in a script exist for the life of that script. Conversely, environment variables (such as $PHP_SELF) exist throughout the server. Functions, though, create a new level of scope. Function variablesthe arguments of a function as well as any variables defined within the functiononly exist within that function and are not accessible outside of it (i.e. they are local variables with local scope). Likewise a variable from outside of a function can only be referenced by passing it to the function as an argument or by using the global statement. The global statement roughly means "I want this variable within the function to be the same as it is outside of the function." In other words, the global statement turns a local variable with local scope into a global variable with global scope. Any changes made to the variable within the function are also passed on to the variable when it is outside of the function ( assuming the function is called, that is), without using the return command. The syntax of the global statement is as follows : function FunctionName ($Argument) { global $Variable; statement(s); } As long as $Variable exists outside of the function, it will also have the same value within the function. This leads to a parallel topic regarding functions and variables: because of variable scope, a variable within a function is a different entity (perhaps with a different value) than a variable outside of the function, even if the two variables use the exact same name (and assuming you do not use the global statement within the function). I'll go over this more explicitly. Frequently you use a function call line like FunctionName ($Value1) ; in which case the function (here, FunctionName ($Argument1) ) then equates the value of $Argument1 to that of $Value1, so their values are the same but their names are different. However, if the name of the argument in the function was also $Value1 (so the function creation line reads FunctionName ($Value1) ), the $Value1 variable within the function assumes the same value as the original $Value1 outside of the function but they are still two separate variables. The one has a scope within the function and the other outside of it. For this reason, I have been careful, when writing functions, to use different variable names in the function defining line than I did in the function call line in order to avoid confusion. I mention this topic because you do not actually have to come up with different names. You could use the exact same name in the function and the call line for convenience sake (it becomes very easy to remember what arguments are passed that way), but remember that they are not the same variable. What happens to a variable's value within a function stays within the function, unless you use the global statement, which does make the two variables the same. In order to better understand this concept, you'll rework the numbers .php script using the global statement. To use the global statement:
|
I l @ ve RuBoard |