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 with the same name in separate functions.
Listing 7.5 creates a variable within a function and then attempts to print it outside the function.
Listing 7.5. Variable Scope: A Variable Declared Within a Function Is Unavailable Outside the Function
Put these lines into a text file called scopetest.php and place this file in your web server document root. When you access this script through your web browser, it should look like Figure 7.3.
Figure 7.3. Output of scopetest.php.
By the Way
The exact output you see depends on your PHP error settings. That is, it may or may not produce a "notice" as shown in Figure 7.3, but it will show the lack of an additional string after "test variable".
The value of the variable $testvariable is not printed because no such variable exists outside the test() function. Remember that the attempt in line 5 to access a nonexistent variable produces a notice such as the one displayed only if your PHP settings are set to display all errors, notices, and warnings; if your error settings are not strictly set, only the string "test variable" will be shown.
Similarly, a variable declared outside a function will not automatically be available within it.
Accessing Variables with the global Statement
From within one function, you cannot (by default) access a variable defined in another function or elsewhere in the script. Within a function, if you attempt to use a variable with the same name, you will only set or access a local variable. Let's put this to the test in Listing 7.6.
Listing 7.6. Variables Defined Outside Functions Are Inaccessible from Within a Function by Default
Put these lines into a text file called scopetest2.php and place this file in your web server document root. When you access this script through your web browser, it should look like Figure 7.4.
Figure 7.4. Attempting to reference a variable from outside the scope of a function.
As you might expect, the meaningOfLife() function does not have access to the $life variable in line 2; $life is empty when the function attempts to print it. On the whole, this is a good thing because it saves us from potential clashes between identically named variables, and a function can always demand an argument if it needs information about the outside world. Occasionally, you may want to access an important variable from within a function without passing it in as an argument. This is where the global statement comes into play. Listing 7.7 uses global to restore order to the universe.
Listing 7.7. Accessing Global Variables with the global Statement
Put these lines into a text file called scopetest3.php and place this file in your web server document root. When you access this script through your web browser, it should look like Figure 7.5.
Figure 7.5. Successfully accessing a global variable from within a function using the global statement.
By placing the global statement in front of the $life variable when we declare it in the meaningOfLife() function (line 4), it now refers to the $life variable declared outside the function (line 2).
You will need to use the global statement within every function that needs to access a particular named global variable. Be careful, though; if you manipulate the contents of the variable within the function, the value of the variable will be changed for the script as a whole.
You can declare more than one variable at a time with the global statement by simply separating each of the variables you want to access with commas:
global $var1, $var2, $var3;
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 carefully.