Table of Contents



Introduction

The scope of a variable is the part of the PHP script where the variable can be referenced or used. There are three different variable scopes:

  • local scope
  • global scope
  • static scope

Local Variable Scope

Local variables are variables that are created within a function and can be accessed only by the function. Possible local variables are

  • variables created within a function.
  • parameters defined in a function.

Example

Local variables defined within a function.

1<?php
2    function testFct($x) {
3        $y = $x+1;
4        return "Inside the function: x is $x and y is $y <br>";
5    }
6    echo testFct(6);
7    echo "The value of x outside the function is $x <br>";
8    echo "The value of y outside the function is $y <br>";
9?>
Inside the function: x is 6 and y is 7

Warning: Undefined variable $x on line 1602
The value of x outside the function is

Warning: Undefined variable $y on line 1603
The value of y outside the function is

Both the variables $x and $y have a local scope within the function. As soon as the function returns, both variables and their data disappear and cannot be accessed outside the function. The difference between $x and $y is that $x is a parameter of the function whereas $y is a variable defined within the function.

Global Variable Scope

A variable declared outside a function has a global scope and can only be accessed outside the function.

Example

A function cannot access a variable defined outside it.

1<?php
2    $increment = 2; // global scope
3    function testFct1() {
4        return "The value of increment within the function is $increment <br>";
5    }
6    echo testFct1();
7    echo "The value of increment outside the function is $increment <br>";
8?>
Warning: Undefined variable $increment on line 1627
The value of increment within the function is

The value of increment outside the function is 2

Note that the variable $increment was defined outside the function and has a global scope. Since $increment was neither created within the testFct1 function nor passed to it as an argument, the function could not access it and a warning was given in the output. Outside the function however, $increment could still be accessed as expected.

The global Keyword

Some global variables may contain data that is large and complex, and we do not want to keep passing them as arguments to functions. The global keyword is used to access a global variable from within a function. We need to declare the variable with the keyword global inside the function.

Example

Using the global keyword within a function.

1<?php
2    $INCREMENT = 2; // global scope
3    function testFct2() {
4        global $INCREMENT;
5        return "The value of increment within the function is $INCREMENT. <br>";
6    }
7    echo testFct2();
8?>
The value of increment within the function is 2.

Or we can also modify the value of the global variable within the function.

Example

Modiying a global variable within a function.

 1<?php
 2    $INCREMENT = 2;
 3    function testFct2() {
 4        global $INCREMENT;
 5        echo "Increment before modification is $INCREMENT. <br>";
 6        $INCREMENT = $INCREMENT +1;
 7        echo "Increment after modification is $INCREMENT. <br>";
 8    }
 9    echo testFct2();
10    echo "Increment outside function is $INCREMENT. <br>";
11?>
Increment before modification is 2.
Increment after modification is 3.
Increment outside function is 3.

The global keyword declaration in the testFct2 function gives it full access to the global variable $INCREMENT and allows the function to modify its value.

However, that doesn’t mean other functions have access to the global variable. The global keyword declaration has to be made in every function that requires access to the global variable.

Example

Using the global keyword in multiple functions.

 1<?php
 2    $INCREMENT = 2;
 3    echo testFct2();
 4    echo testFct3();
 5    echo "Increment outside function is $INCREMENT. <br>";
 6    function testFct2() {
 7        global $INCREMENT;
 8        $INCREMENT = $INCREMENT + 1;
 9        return "The value of increment within 'testFct2' is $INCREMENT. <br>";
10    }
11    function testFct3() {
12        global $INCREMENT;
13        $INCREMENT = $INCREMENT + 1;
14        return "The value of increment within 'testFct3' is $INCREMENT. <br>";
15    }
16?>
The value of increment within 'testFct2' is 3.
The value of increment within 'testFct3' is 4.
Increment outside function is 4.

Static Variables

When a function has completed execution, all of its local variables are deleted. If a function is called multiple times, it starts with a fresh copy of the variable every time, and its previous value is forgotten.

However, what if you have a local variable inside a function where you would like to keep its value for the next time the function is called? To do this, we can use the static keyword when we declare the variable in the function.

Example

Using the static keyword within a function.

 1<?php
 2    function test(){
 3        static $count = 0;
 4        echo $count . "<BR>";
 5        $count++;
 6    }
 7    test();
 8    test();
 9    test();
10?>
0
1
2

During the first call to the function test(), the first line creates a static variable called $count and initializes it to a value of 0. The next line outputs the variable’s value and the last line increments it.

Subsequent calls to the function will ignore the first line since $count has already been declared in the first call.

Note that we could also have achieved the same results using the global keyword.

Example

Using the global keyword within a function.

 1<?php
 2    $count = 0;
 3    function test1(){
 4        global $count;
 5        echo $count . "<BR>";
 6        $count++;
 7    }
 8    test1();
 9    test1();
10    test1();
11?>
0
1
2

It is important to note that $count is now a global variable whereas $count in the previous example is a local variable.