JavaScript Editor Javascript source editor     Web programming 



Team LiB
Previous Section Next Section

Variable Scope

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
1: <?php
2: function test() {
3:      $testvariable = "this is a test variable";
4: }
5: echo "test variable: $testvariable <br>";
6: ?>

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.


The value of the variable $testvariable is not printed, because no such variable exists outside the test() function. Note that the attempt in line 5 to access a nonexistent variable does not cause an error.

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
1: <?php
2: $life = 42;
3: function meaningOfLife() {
4:      echo "The meaning of life is $life.";
5: }
6: meaningOfLife();
7: ?>

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, as 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
1: <?php
2: $life=42;
3: function meaningOfLife() {
4:      global $life;
5:      echo "The meaning of life is $life<br>";
6: }
7: meaningOfLife();
8: ?>

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 keyword.


By placing the global keyword 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 for a particular global variable. Be careful, though, because 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 wish to access with commas:

global $var1, $var2, $var3;

Watch out!

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.


    Team LiB
    Previous Section Next Section


    JavaScript Editor Javascript source editor     Web programming

    
    R7