Why does the * RECURSION * line appear in the output, and each array name it contains begins with an underscore in the output of the $ GLOBALS array?

I am using the following PHP version on my local machine and only run PHP programs from localhost.

PHP 5.3.10-1ubuntu3.13 with Suhosin-Patch (cli) (built: Jul  7 2014 18:54:55) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

      

I wrote a very simple PHP program like this:

<!DOCTYPE html>
<html>
  <body>

  <?php
    $x=5;
    $y=10;

    print_r($GLOBALS);
    die;

    function myTest() {
      global $x,$y;
      $y=$x+$y;
    }


    myTest(); // run function

    echo $y; // output the new value for variable $y
  ?>

  </body>
</html>

      

After running the above program, I get the following output in the browser window:

Array ( [GLOBALS] => Array *RECURSION* [_POST] => Array ( ) [_GET] => Array ( ) [_COOKIE] => Array ( ) [_FILES] => Array ( ) [x] => 5 [y] => 10 )

      

From the above output, I don't understand why the RECURSION line is displayed, where does it come from, what is behind it?

Also, why does the underscore appear at the beginning of all ** (_ POST, _GET, _FILES) ** arrays it contains?

There are so many doubts in my head. Can someone clarify my doubts?

+3


source to share


1 answer


The easiest way to think about it is to imagine what it would be like if it were *RECURSIVE*

n't displayed:

Array 
( 
    [GLOBALS] => Array 
        (
            [GLOBALS] = Array
                (
                      [GLOBALS] = Array
                          (
                                  ...
                          )
                      ...
                )
            [_POST] => Array ( )
            [_GET] => Array ( )
            ...
        ) 
    [_POST] => Array ( ) 
    [_GET] => Array ( ) 
    [_COOKIE] => Array ( ) 
    [_FILES] => Array ( ) 
    [x] => 5 
    [y] => 10 
)

      

$GLOBALS

contains references to all global variables, and since it $GLOBAL

is the global variable itself, it also contains a reference to itself. As you can see, the above will result in a recursive pattern that never ends, simply because the $GLOBALS

-array contains a reference to itself as an element. This means that printing using is print_r()

not possible, as it will lead to infinite recursion. So the function print_r()

solves this by adding a line instead *RECURSION*

. Here's a quote from the PHP manual :

Prior to PHP 4.0.4, print_r () will continue forever if given an array or object containing a direct or indirect reference to itself. An example is print_r ($ GLOBALS), because $ GLOBALS is itself a global variable that contains a reference to itself.



This also means that displaying any object that contains recursive references to itself will result in that string being displayed. Simple example:

$a = array(&$a); //$a contains one element with a reference to itself
print_r($a); //prints: Array ( [0] => Array ( [0] => Array *RECURSION* ) ) 

      

Note that this will not hide any information from you - you still know what the element contains GLOBALS

(the same as the printed one, basically), if that's what you mean.

To answer your second question, the reason names and underscores start is simply because multiple "superglobals" are doing it. This makes it easier to extract super-globals from regular variables and (somewhat) allows people to misuse these names. So when you access _POST

-array, for example, you use $_POST

instead $POST

, which means that the item's key in $GLOBALS

-variable becomes _POST

, since that's the "actual" name of the variable.

+1


source







All Articles