du21064 2014-08-24 06:02
浏览 81
已采纳

为什么字符串* RECURSION *出现在输出中,并且每个包含的数组名称都是在$ GLOBALS数组输出中使用下划线字符启动的? [重复]

This question already has an answer here:

I'm using following PHP version on my local machine and running the PHP programs from localhost only.

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've written a very simple PHP program as follows:

<!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 executing the above program I'm getting following output in a browser window:

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

From the above output I'm not getting why the string RECURSION is getting displayed, from where it came. What's the cause behind it?

Also, why is the underscore character appearing at the beginning of all contained arrays**(_POST, _GET, _FILES)**?

</div>
  • 写回答

1条回答 默认 最新

  • duanbei7005 2014-08-24 11:03
    关注

    The easiest way to think of it is by imagining how it would have been if *RECURSIVE* WOULDN'T have been 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 $GLOBAL is a global variable itself, it also contains a reference to itself. As you can see, the above will result in a recursive pattern, which never ends, simply because the $GLOBALS-array contains a reference to itself as an element. This means that printing it with print_r() would be impossible, since it would result in infinite recursion. Therefore, the print_r() function solves this by adding the string *RECURSION* instead. 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 that contains 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 which contains recursive references to themselves will lead to this string being displayed. A simple example is:

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

    Although note that this won't hide any info from you - you still know what the GLOBALS-element contains (the same thing as printed, basically), if having all this in mind.

    To answer your second question: The reason the names start with and underscore is simply because several "super-global" variables do so. It makes it easier to distinguish the super-globals from regular variables, and (somewhat) prevents people from using those names by mistake. So when accessing the _POST-array for instance, you use $_POST instead of just $POST, meaning that the element-key in the $GLOBALS-variable becomes _POST, as it's the "actual" name of the variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图