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 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办