From docs :
Using a global keyword outside a function is not a mistake. It can be used if the file is included inside the function.
Essentially, you can have your function in a different file than the declaration of your function. These guts will then be included in the function. This will give the impression that if you look only at the guts of a user using global outside the function, the fact is that when this code is interpreted, it will be interpreted from within the function.
$name = "Jonathan"; function doSomething () { include( 'functionGuts.php' ); }
If the contents of our functionGuts.php file could be:
global $name; echo "Hello, " . $name;
When viewed on its own, functionGuts.php will give the impression that global used outside the function, when in fact it is used as follows:
$name = "Jonathan"; function doSomething () { global $name; echo "Hello, " . $name; }
Sampson
source share