What does * RECURSION * mean when printing $ GLOBALS?

When I print $GLOBALS with this code:

 <?php print_r($GLOBALS); ?> 

I get this output:

Array ( [_GET] => Array ( ) [_POST] => Array ( ) [_COOKIE] => Array ( ) [_FILES] => Array ( ) [GLOBALS] => Array *RECURSION* )

What does *RECURSION* in this case and why $_SERVER , $_REQUEST , etc. not printed either?

+7
source share
3 answers

See this part of the PHP manual :

Keep in mind that $ GLOBALS is the most global variable. Therefore, such code will not work:

 <?php print '$GLOBALS = ' . var_export($GLOBALS, true) . "\n"; ?> 

The result is an error message: "Nesting level too deep - recursive dependency?"

You have already received the entire list — you simply cannot display part of it (the one that contains the recursion, because you will have a timeout, not something meaningful).

When it comes to $_REQUEST , it is derived from $_GET , $_POST and $_COOKIE , so its contents are redundant.

EDIT : An old bug / function that seems to populate $GLOBALS $_SERVER and $_REQUEST when available. So try accessing $_REQUEST and hope this helps. Anyway, it can be found in $GLOBALS after that: ideone.com/CGetH

+4
source

$GLOBALS contains itself as an array. In the PHP link, you can find the definition of $GLOBALS :

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

Therefore, it must also contain itself, which leads to recursion.

Other arrays are probably just empty, since nothing happened in your script.

There is an old recursion joke: "To understand recursion, you must understand recursion."

BTW: it outputs _SERVER on my computer.

+2
source

When you have an object pointing to itself ... i.e. itll just in a circle.

0
source

All Articles