我们来先看一段代码
代码语言:javascript复制<?php
$a = "string";
$b = &$a;
echo $a;
echo $b;
$b = "hello";
echo $b;
echo $a;
unset($b);
echo $b;
echo $a;
?>
输出结果为
string
string
hello
hello
(空)
hello
为什么会输出这样的结果呢?我们来分析一下 首先我们看一下引用类型的结构
代码语言:javascript复制struct _zend_reference {
zend_refcounted_h gc;
zval val;
};
我们可以看到,引用类型是一个变量zval和一个zend_refcounted_h组成 先看第一段的 a = "string";
代码语言:javascript复制(gdb) p *z
$1 = {value = {lval = 140737314300072, dval = 6.9533472083627576e-310, counted = 0x7ffff5a020a8, str = 0x7ffff5a020a8, arr = 0x7ffff5a020a8, obj = 0x7ffff5a020a8,
res = 0x7ffff5a020a8, ref = 0x7ffff5a020a8, ast = 0x7ffff5a020a8, zv = 0x7ffff5a020a8, ptr = 0x7ffff5a020a8, ce = 0x7ffff5a020a8, func = 0x7ffff5a020a8, ww = {w1 = 4120912040,
w2 = 32767}}, u1 = {v = {type = 10 'n', type_flags = 4 '