There is A LOT concerning proper global variables use, and I'm about to request further clarification. Many of the posts dating back 3 years asked, "How do I make variables included in a function, global in scope."
My assumption is that their code looks like this, since no explicit examples were given:
Example 1
#global_vars.php
<?
$my_global_var = "Hello World";
?>
#index.php
<?
function foo ()
{
include_once ("global_vars.php");
global $my_global_var;
print ("my global var = [" . $my_global_var . "]<BR>");
print ("complete<BR>");
}
foo ();
?>
which outputs:
my global var = []
complete
Example 2
Now, if "globals_var.php" is changed to:
<?
global $my_global_var;
$my_global_var = "Hello World";
?>
The results are:
my global var = [Hello World]
complete
Example 3
Now before you write and state that this type of global variable use is not advised, there are practical uses of global variables. And that most global variable are encapsulated in objects as a constant or as a function:
#global_vars.php
<?
class Globals
{
const my_global_var = "Hello World";
public static $my_global_var2 = "Hello World";
static function my_global_var3 ($newValue = null)
{
if (isset ($newValue))
self::$my_global_var2 = $newValue;
return (self::$my_global_var2);
}
}
?>
#index.php
<?
function foo ()
{
include_once ("global_vars.php");
print ("my global var = [" . Globals::my_global_var . "]<BR>");
print ("my global var = [" . Globals::my_global_var3() . "]<BR>");
print ("my global var = [" . Globals::my_global_var3("Hi There") . "]<BR>");
print ("my global var = [" . Globals::my_global_var3() . "]<BR>");
print ("complete<BR>");
}
foo ();
?>
The output is:
my global var = [Hello World]
my global var = [Hello World]
my global var = [Hi There]
my global var = [Hi There]
complete
Example 4
Of course at this point the "include_once" statement could be moved outside the function to get the same results.
#index.php
<?
include_once ("global_vars.php");
function foo ()
{
print ("my global var = [" . Globals::my_global_var . "]<BR>");
print ("my global var = [" . Globals::my_global_var3() . "]<BR>");
print ("my global var = [" . Globals::my_global_var3("Hi There") . "]<BR>");
print ("my global var = [" . Globals::my_global_var3() . "]<BR>");
print ("complete<BR>");
}
foo ();
?>
The output:
my global var = [Hello World]
my global var = [Hello World]
my global var = [Hi There]
my global var = [Hi There]
complete
Example 5
In this example, the file is included outside the function. Everything works as explained by the PHP.net documentation.
#global_vars.php
<?
$my_global_var = "Hello World";
?>
#index.php
<?
include_once ("global_vars.php");
function foo ()
{
global $my_global_var;
print ("my global var = [" . $my_global_var . "]<BR>");
$my_global_var = "Hi There";
print ("my global var = [" . $my_global_var . "]<BR>");
}
foo ();
?>
Question ...
Now for my question ... If there is no namespace, why are variables included in Example 1 not to placed in $GLOBALS? And, why are these variables not considered part of the function scope? What's going on here?
Example 1 - GLOBALS / Defined Vars
#index.php
<?
function foo ()
{
include_once ("global_vars.php");
global $my_global_var;
print ("my global var = [" . $my_global_var . "]<BR>");
print ("<pre>");
print_r ($GLOBALS);
print ("</pre>");
print ("<pre>");
print_r (get_defined_vars ());
print ("</pre>");
}
foo ();
?>
The output is:
my global var = []
Array
(
[_GET] => Array ()
[_POST] => Array ()
[_COOKIE] => Array ( ... )
[_FILES] => Array ()
[GLOBALS] => Array
*RECURSION*
[my_global_var] =>
)
Array
(
[my_global_var] =>
)
Example 2 - GLOBALS / Defined Vars
my global var = [Hello World]
Array
(
[_GET] => Array ()
[_POST] => Array ()
[_COOKIE] => Array ( ... )
[_FILES] => Array ()
[GLOBALS] => Array
*RECURSION*
[my_global_var] => Hello World
)
Array
(
[my_global_var] => Hello World
)
Example 6
Based on "@kainaw" answer I have updated Example 1 to help others understand what this would look like based on the examples given. Assume that nothing changed in "global_vars.php", but that in the "index.php" file the global statement preceded the include statement. It is then that "my_gloval_var" is considered by PHP to be a global variable, and initialized as such.
#global_vars.php
<?
$my_global_var = "Hello World";
?>
#index.php
<?
function foo ()
{
global $my_global_var;
include_once ("global_vars.php");
print ("my global var = [" . $my_global_var . "]<BR>");
print ("complete<BR>");
}
foo ();
?>
which outputs:
my global var = [Hello World]
complete
The $GLOBALS and Define Vars return the following:
my global var = [Hello World]
Array
(
[_GET] => Array ()
[_POST] => Array ()
[_COOKIE] => Array ( ... )
[_FILES] => Array ()
[GLOBALS] => Array
*RECURSION*
[my_global_var] => Hello World
)
Array
(
[my_global_var] => Hello World
)