Global Scope
The global scope spans all included files. That is, $foo
will be visible in file.php.
<?php
$foo = 1;
include 'file.php';
?>
Function Local Scope
If you wish to access a global variable from within a function, you have to
use the global keyword to tell the function that
it should use the variable from the global scope instead of the function's own
local scope.
<?php
function bar() {
global $foo;
echo $foo;
}
$foo = 1;
bar();
?>
Output
Static Variables
<?php
function bar2() {
static $i=0;
return ++$i;
}
echo bar2()."<br>\n";
echo bar2()."<br>\n";
?>
Output