Broad Network


PHP Variable Scope Basics

Basics of PHP – Part 12

Forward: I explain the basics of PHP variable scope in this article.

By: Chrysanthus Date Published: 28 Jul 2012

Introduction

This is part 12 of my series, Basics of PHP. When you declare a variable outside a function, it may not be seen in the function. When you declare a variable inside a function, it may not be seen outside the function. That feature is called variable scope. I explain the basics of PHP variable scope in this article.

You need basic knowledge in PHP in order to understand this series. If you do not have that prerequisite knowledge, then read the series I wrote in this blog titled,

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Passing Arguments
Consider the following code:

    <?php

        $a = 4;

        function sendValue($x)
            {
             echo $x;
            }

        sendValue($a);

    ?>

In the above code, the variable, $a is declared outside the function. The definition of the function, simply sends the value of its argument to the browser. When the function is called, the variable, $a is sent as argument. This value is echoed. Now note two things: This variable is declared outside the function. It is passed to the function as an argument. In the function definition, the variable echoed is the parameter variable of the function and not the variable declared outside the function. As the value of the variable, declared outside the function is passed as argument, in the definition of the function, this value becomes the value of the parameter variable.

When a variable is declared outside a function and passed as argument to the function, the definition of the function sees the variable. The above code works. Now, try the following code and note that it does not work:

    <?php

        $a = 4;

        function sendValue()
            {
             echo $a;
            }

        sendValue();

    ?>

Here, the variable is still declared outside the function. The function does not have any parameter. When the function is called, the variable is not sent as an argument. However, in the function definition, the variable declared outside, and not the parameter variable, is expected to be echoed. In some computer languages, the above code will work. In PHP, it does not work because a PHP function cannot see a variable declared outside its definition; that is just the rule of PHP.

Global and Local Variables
In PHP, any variable declared outside a function as $a above, is a global variable. In PHP any variable declared inside a function (see below), is a local variable. In the following code, the $a declared outside the function and the $a declared inside the function are entirely two different things. Read and try the following code:

    <?php

        $a = 4;

        function sendValue()
            {
             $a;
             echo $a;
            }

        echo "Value of variable outside<br />";
        echo $a; echo "<br />";

        echo "Value of variable inside<br />";
        sendValue();

    ?>

As you can see from the result, the two variables, though having the same name, but by the fact that one is outside the function and the other is inside, would hold different values. The one inside the function in this case, did not even acquire a value.

The reserved word, global
If you want the variable declared outside a function to hold the same value as the one inside the function, you have to re-declare the one inside the function, preceding it with the reserved word, global, as in the following code:

    <?php

        $a = 4;

        function sendValue()
            {
                global $a;
                echo $a;
            }

        echo $a; echo "<br />";
        sendValue();

    ?>

From the result, the two values displayed are the same. This illustrates the use of the reserved word, global.

Superglobals
PHP has variables called super global variables, or simply, Superglobals. These are variables that can be seen outside and inside functions without the use of the reserved word, global. The programmer is not allowed to declare such variables. There are only some of them available and they are predefined. Examples of superglobals are the $_POST and $_GET variables. These are actually arrays, not variables.

Note: When a global variable is passed as argument to a function, the function definition will see the value of the variable; however, this does not make the global variable, local or superglobal.

There are other things to learn about variable scope, but for the purpose of this tutorial, we shall end here. We continue with something else in the next part of the series.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message