duanjiao5723 2017-09-28 06:11
浏览 43
已采纳

引用不是php中的指针?

I am new to php and learning it from php.net. Can anyone tell in the summary why does that page (http://php.net/manual/en/language.references.arent.php) says What references are not & References are not pointers.

I am beginner so please can anyone explain me in simple and easy words ?

  • 写回答

2条回答 默认 最新

  • duanji2772 2017-09-28 06:23
    关注

    In simple words, references are aliases.

    A variable in PHP is stored in two pieces: the name and the value. The name points to the value.

    $x = 2;
    $y = $x;
    $z = &$x;
    

    When $x = 2; is executed, the name x is stored in the symbol table of the current scope and the value 2 is stored in a zval (don't ask, it's the internal name of a data structure that stores a value in PHP).

    When $y = $x; is executed, the name y is stored in the symbol table of the current scope and the value of $x (2) is copied into a new zval structure.

    When $z = &$x; is executed, the name z is stored in the symbol table of the current scope but a new zval is not created. Instead, z is set to point to the same zval as x.

    The memory used by the variables $x, $y and $z looks like this:

    +---------+                +---------+
    |    x    | -------------> |    2    |
    +---------+                +---------+
                                    ^
    +---------+                     |      +---------+
    |    y    | -------------------------> |    2    |
    +---------+                     |      +---------+
                                    |
    +---------+                     |
    |    z    | --------------------+
    +---------+
    

    When a value is passed by reference to a function or a function returns a reference the same things happen, only the names are stored in different symbol tables (remark the "current scope" in the explanation above).

    Let' see this code:

    function f(& $z) {
        $y = $z;
        $z = $z + 2;
    }
    $x = 2;
    f($x);
    

    After $x = 2; the memory looks like this:

    +---------+                +---------+
    |    x    | -------------> |    2    |
    +---------+                +---------+
    

    During the execution of function f(), the memory looks like this:

    +===== global ====+
    |   +---------+   |            +---------+
    |   |    x    | -------------> |    4    |
    |   +---------+   |            +---------+
    +=================+                 ^
                                        |
    +====== f() ======+                 |
    |   +---------+   |                 |      +---------+
    |   |    y    | -------------------------> |    2    |
    |   +---------+   |                 |      +---------+
    |                 |                 |
    |   +---------+   |                 |
    |   |    z    | --------------------+
    |   +---------+   |
    +=================+
    

    y and z are stored in a different symbol table than x and they are removed (together with the entire symbol table that contains them) when the call to f() returns.

    When y is removed, its value is also removed because there is no name that points to it any more. But, because the value pointed by z is also pointed by x ($z is an alias), the value is not removed together with z and it survives the function call. f() modifies the value using $z; and this change is visible in the main program through the variable $x.

    The things happen in a similar way when a function returns a reference. The function returns a value that is not copied but a new name that points to it is created into the symbols table of the calling code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi