Copyright © tutorialspoint.com

PHP Global Variables

previous

Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types:

Global Variables:

In contrast to local variables, a global variable can be accessed in any part of the program. However, in order to be modified, a global variable must be explicitly declared to be global in the function in which it is to be modified. This is accomplished, conveniently enough, by placing the keyword GLOBAL in front of the variable that should be recognized as global. Placing this keyword in front of an already existing variable tells PHP to use the variable having that name. Consider an example:

<?
$somevar = 15;
function addit() {
GLOBAL $somevar;
$somevar++;
print "Somevar is $somevar";
}
addit();
?>

This will produce following result.

Somevar is 16
previous

Copyright © tutorialspoint.com