$GLOBALS: It contains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables.

Syntax: $GLOBALS

Example1:
Code:

function check() {
   $scope = "local variable";

    echo '$scope in global scope: ' . $GLOBALS["scope"] . "\n";
    echo '$scope in current scope: ' .$scope . "\n";
}

$scope = "Global variable";
check();

Output:

$scope in global scope: Global variable
$scope in current scope: local variable

Example2:
Code:


$x = 10;
$y = 5;

function change()
{
   $x += 5;
}

function change_global()
{
   global $y;

   $y += 5;
}

change();
change_global();
print "x: $x 
"; print "y: $y
";

Output:

x: 10
y: 10

Explanation:

The function change() cannot access the variable x, because it is not in the global scope.

eXTReMe Tracker