The concept of variable scope was not introduced in Chapter 2, Variables, because without an understanding of functions, scope makes little sense. Now that you have a reasonable familiarity with functions, we will revisit the topic of variables and discuss in some detail just how variables and functions work together.
As you saw in Exercise 2: 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. In contrast, environment variables (such as $_SERVER[‘PHP_SELF’]) exist throughout the server.
Functions, though, create a new level of scope. Function variables—the arguments of a function as well as any variables defined within the function—exist only within that function and are not accessible outside of it (that is, they are local variables with local scope). Likewise, a variable from outside 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:
As long as $variable exists outside of the function, it will also have the same value within the function.
This leads to another issue 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). Let’s go over this more explicitly.
Frequently, you will use a function call line like this one:
The function [here, function_name ($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 is also $value1 [so the function-creation line reads function_name($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. One has a scope within the function, and the other has a scope outside of it.
So, you do not have to come up with different names. You can use the exact same name in the function and the call line for the sake of convenience (it becomes 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 this exercise you will rework the calculator.php script using the global statement.
Script 10-5: The function in this script can use the $tax variable—even though it hasn’t been passed to the function—thanks to the global statement.
Links
[1] https://www.studyanywhere.ca/advanced-courses/intro-php/chapter-10/creating-functions/exercise-5
[2] https://www.studyanywhere.ca/advanced-courses/intro-php/chapter-10/creating-functions/exercise-5#open_here
[3] https://www.studyanywhere.ca/advanced-courses/intro-php/chapter-10/creating-functions/exercise-5#open_here2
[4] https://www.studyanywhere.ca/advanced-courses/intro-php/chapter-10/creating-functions