Understanding the PHP $GLOBALS variable Understanding the PHP $GLOBALS variable arrays arrays

Understanding the PHP $GLOBALS variable


From my knowledge of PHP and doing some research as well as testing this on multiple OS' with various version of PHP I found the following.

Question 1 & 3:

Yes you are correct with regards to the 9 superglobals, but a very important thing to keep in mind is that $GLOBALS -- References all variables available in global scope.

An interesting sidenote, notice that $GLOBALS is the only superglobal that doesn't start with an underscore.

Because of the fact that $GLOBALS contains references to all the other superglobals including itself, when we print_r($GLOBALS) it will also include the other superglobals in the output. Because $GLOBALS references itself as well we get the RECURSION you asked about in your 3rd point. You can think of it as a infinite dimensional array containing $GLOBALS. Almost the same idea as an infinte loop.

[GLOBALS] => Array    (        [GLOBALS] => Array            (                [GLOBALS] => Array                    (                        ...                    )            )    )

Instead the script sees this and stop executing and just prints RECURSION. Now I have tested it on 3 different environments and each time the order in which the superglobals are printed changed, but as soon as it hits $GLOBALS it stops and prints RECURSION.

Question 2:

I could not find any info on $_COOKIE[toWorkNormally] => 1. I am assuming this is set somewhere else. I didn't see it in any of my tests.

Question 4:

This is neither correct nor incorrect. The purpose of $GLOBALS is not to store all variables created by the user globally. It merely references all variables available in global scope including, the superglobals. That is why you are seeing all the other superglobals in the output. But a lot of developers assume that the user defined global variables are stored in $GLOBALS.

Description in the PHP.net manual

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.

To view all the superglobals you will have to print_r() each one of them individually.

To check all user defined global variables you can use array_keys($GLOBALS) all the items which are not superglobals will most probably be user defined global variables.

EDIT in response to users comments

In response to your 1st comment, No they are not different. The superglobals not printed are still part of the array but execution/output stops because it hits the RECURSION when it gets to $GLOBALS. The superglobals are printed in a random order and which ever comes after the $GLOBALS will not be seen as it detects a RECURSION at $GLOBALS and stops the output.

You can check all the superglobals/global variables by using print_r(array_keys($GLOBALS)); With an exception of $_SESSION because a session has not been started yet. print_r($_SESSION) will give you an undefined variable $_SESSION Notice. You will be able to see $_SESSION when you put session_start(); just before you print it.

Link to What References Are in PHP

References in PHP are a means to access the same variable content by different names.

Note that in PHP, variable name and variable content are different, so the same content can have different names


The PHP manual says the following about the $GLOBALS variable:

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.

This describes exactly what the variable does. It is simply a reference to existing variables.

The RECURSION you are talking about is the $GLOBALS variable referring to itself. Since we don't want PHP to endlessly output the same output and crashing your server in the process, there is a built-in failsafe that shows you the RECURSION alert if this is the case.

I would like to add that $GLOBALS is a superglobal, or preset global, variable. This means that it is available in all scopes throughout your script.

Resources


$GLOBALS is the global of all super global and user defined variables. So for example if you have declared variable $a = 10; in your $GLOBALS array you have key=>value pair where key is a and value is 10.If you want to get something from $GLOBALS you just need to write it as array key.

example

$a = 25;echo $GLOBALS['a'];

In the example above output will be the value of $a so 25;

In your example toWorkNormally=>1 it`s mean that you have set cookie with name toWorkNormally and with value 1 or true

Also when you submit form with get or post method in the $GLOBALS['_GET'] or $GLOBALS['_POST'] there you can find your form data as you can get them from super global $_GET or $_POST