Broad Network


Global and Function Variable Scope in PHP

Understanding Variable Scope in PHP – Part 1

Forward: In this part of the series, we look at global and function variable scope in PHP.

By: Chrysanthus Date Published: 10 Aug 2012

Introduction

This is part 1 of my series, Understanding Variable Scope in PHP. In this part of the series, we look at global and function variable scope in PHP. Everything in this series concerns PHP 5.

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.

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 whose first part is titled, Getting started with PHP. To reach the series, just type the title and my name Chrys in the Search Box of this page and click Search.

What is a Variable Scope
The PHP specification, states this: “The scope of a variable is the context within which it is defined.” You may not understand what that means if you are new to computing. So I will do some illustration. Consider the following program:

<?php

    function fn()
        {
            $b = 2;
            echo $b;
        }

    fn();

?>

The variable, $b is declared and used only in the function block. This variable cannot be seen outside the function (block). This variable is said to have Local Function Scope or simply, Local Scope. Consider now the following program:

<?php

    $b = 2;

    function fn()
        {
            //some statements . . .
        }

    echo $b;

?>

The variable, $b is declared and used outside the function. This variable cannot be seen inside the function. It is said to have Global Scope.

Seeing Global Variable in Local Scope
Ordinarily, you cannot see a global variable in local scope and you also cannot see a local variable in global scope. However, it is possible to make a global variable appear in local scope and not vice-versa. There are two ways to make a global variable appear in local function scope as follows:

Use of the global Keyword
Assume that a global variable has been declared outside the function. In order for that global variable to be seen inside the function, you have to re-declare it inside the function preceded by the keyword, global. The following program illustrates this:

<?php

    $b = 2;

    function fn()
        {
            global $b;
            echo $b;
        }

    fn();

?>

With the global variable re-declared inside the function preceded by the word, global, the global variable can be seen inside the function.

Use of the predefined $GLOBALS Array
There is a predefined array called $GLOBALS. This is an associative array with key/value pairs. All the global variables and their values in your program are stored in this array as key/value pairs. For each global variable, the name of the variable forms the key and the value of the variable forms the corresponding array value. The secret is that this array can be seen inside a function as well as outside. So if you want a global variable inside a function, just access the corresponding element for the global variable in the array. The following program illustrates this:

<?php

    $b = 2;

    function fn()
        {
            echo $GLOBALS['b'];
        }

    fn();

?>

Note that as key for the global variable in the array, you do not have the $ sign, preceding the name of the variable. The name of the variable is in quotes as key in the square brackets, such as with ‘b’ above.

Passing a Global Variable to a Function
Consider the following code that works:

<?php

    $c = 2;

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

    fn($c);

?>

In the above code, $c is a global variable and it cannot be seen inside the function. The function has the parameter, $x. In the function call, the global variable is passed as argument. The function works. This does not mean that the global variable has been seen by the function. The global variable has not been seen by the function because the keyword, global or the $GLOBALS array has not been used.

Here is the explanation of what has happened: After the function has just been called, immediately as the execution of the function starts, the value of the parameter, $x becomes the value of the global variable, $c. The function definition block uses $x and not $c. So the value of the global variable has been passed to the function without the function seeing the global variable (name). It is the value of the global variable that is sent as argument and not the variable (name).

Superglobals in PHP
There are some predefined arrays that can be seen in both global and function scopes. In order to use any of these arrays in a function, you do not need to precede it with the keyword, global. You just go ahead and use it by typing its name including a key to get a value. An example of such an array is $GLOBALS that we have just seen. These arrays are called Superglobals. In PHP, superglobals are:

$GLOBALS
$_SERVER
$_GET
$_POST
$_FILES
$_COOKIE
$_SESSION
$_REQUEST
$_ENV

I have just talked about $GLOBAL, which is the first in the list. To know about the others, you should consult some other documents.

Non Function Blocks
Are there conditional blocks, such as the if-block as local scopes? No, there are not. Local scope is normally concerned with functions. Consider the following code:

<?php

    $c = 2;

    if ($c == 2)
        {
            $c = 3;
            echo $c;
        }    

?>

$c is a global variable. It is seen in the if-block and even modified. So the if-block here does not form a local scope. Conditional blocks do not determine scopes.

We now know the difference between the global scope and the local scope. With the keyword, global or the $GLOBALS array, we can see the global scope variable in a local function scope. Let us end here and continue in the next part of the series.

Chrys

Related Links

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

Comments

Become the Writer's Fan
Send the Writer a Message