Writing Functions In PHP – Global Variable

Two see the previous tutorials click the following links.

“Writing Functions In PHP – Introduction”.

“Writing Functions In PHP – Flexibility”.

“Writing Functions In PHP – Return Values”.

Global Scope Variable vs Local Scope Variable

Take a look at the following code, and give it your best guess whether it’s going to come back with “outside” or “inside”?

<html>
<head>
<title>Writing Functions In PHP - Global Variable</title>
</head>
<body>
    <?php

        $bar = &quot;outside&quot;; // global scope variable

        function foo() {
            $bar = &quot;inside&quot;; // local scope variable
        }

        foo();
        echo $bar . &quot;<br>&quot;;

    ?>
</body>
</html>

Output: outside

Why does it output “outside” instead of “inside”?

This is because the variable $bar (local scope variable) inside of the function has no relationship to the variable $bar (global scope variable) that was on the outside of the function. The variable inside of the function doesn’t know what value $bar is and has nothing to return out of the function. So it took the original value of $bar which is global scope variable that is “outside”. Let us see another example.

Now, take a guess whether it’s going to come back with “outside” or “inside” with this one?

<html>
<head>
<title>Writing Functions In PHP - Global Variable</title>
</head>
<body>
    <?php

        $bar = &quot;outside&quot;; // global scope variable

        function foo2($var) {
            $var = &quot;inside&quot;; // local scope variable
            return $var;
        }

        $bar = foo2($bar);
        echo $bar . &quot;<br>&quot;;

    ?>
</body>
</html>

Output: inside

Yes, it’s going to display “inside” for this function. We’ve set the value for the variable $bar to “outside”, pass it as an argument into the function foo2(). The function received it and reset them with the value “inside”, a function’s local variable, and later on return back the value out of the function which will be assigned to the variable $bar.

There is another way that we can do this kind of variable declaration, that is by using Globals.

Global Variable Declaration

Referring back to our first example,

    <?php

        $bar = &quot;outside&quot;; // global scope variable

        function foo() {
            $bar = &quot;inside&quot;; // local scope variable
        }

        foo();
        echo $bar . &quot;<br>&quot;;

    ?>

The variable $bar which is on the outside of the function is considered as a global variable. It has a global scope. It can be accessed anywhere inside the same page that we are working with. What ever happens inside the function is considered a local variable. It’s local only to the function and it doesn’t exist in the global scope. Anyway, we can change that by simply saying “Global $bar” inside of the function.

    <?php

        $bar = &quot;outside&quot;;

        function foo() {
            global $bar; // declaring global variable
            $bar = &quot;inside&quot;; // local scope variable
        }

        foo();
        echo $bar . &quot;<br>&quot;;

    ?>

Output: inside

What we are doing here is that we’ve told it to pull in that global variable $bar, go outside of the function and grab the variable for $bar, the global scope and pull that in so we can use it inside the function.

When the function finished execution, any changes that we’ve made to the variable $bar inside the function are going to be to that variable, no matter whether if it is a global scope variable or a local scope variable. In other words, the changes that we’ve made to the variable $bar inside the function are going to reset any other variable $bar outside of the function.

Method Comparison

When using global variable, you want to be careful because once you’ve declared something global, you’re effecting what happens outside of the function. So my advice is, use it with caution. You might be better off, passing in a variable into the function like we did in the second example.

If you are sure that you want to use your variable globally, then it is fine to declare your variable as global, but if you want to keep track on the values as they move around your script then you will declare your variable as a local variable. It’s a stylish choice that you’ll have to make. Here is the functions comparison that produces the same end result.

<html>
<head>
<title>Writing Functions In PHP - Global Variable</title>
</head>
<body>

    <?php

        /* using global variable */

        $bar = &quot;outside&quot;;

        function foo() {
            global $bar; // declaring global variable
            $bar = &quot;inside&quot;; // local scope variable
        }

        foo();
        echo $bar . &quot;<br>&quot;;

    ?>

    <br>

    <?php

        /* using local variables, argumants and return values */

        $bar = &quot;outside&quot;; // global scope variable

        function foo2($var) {
            $var = &quot;inside&quot;; // local scope variable
            return $var;
        }

        $bar = foo2($bar);
        echo $bar . &quot;<br>&quot;;

    ?>

</body>
</html>

Now we are ready to look at how we can set some default values to the arguments that can pass into a function so that a function can have some reasonable default values in case the user doesn’t pass in information or an argument into the function.

We will do that in the next tutorial in this series which is the final topic that we will be discussing about php function.

4 Comments on "Writing Functions In PHP – Global Variable"

  1. Monie says:

    Another topic that I did not cover in this tutorial is PHP Super Global variable. Perhaps you can check that out?

  2. Rich97 says:

    Good tutorial. :)

    Just remember people local > global. Otherwise Object Orientation wouldn’t exist!

    Roll on PHP5.3 :)

  3. Devin says:

    Great write up, I needed a quick a dirty definition and you delivered.

  4. Dave Kiss says:

    Thank you for this! Came through for me.

Got something to say? Go for it!