Home >Backend Development >PHP Tutorial > 关于引用, 这种情况哪位高手能解释下

关于引用, 这种情况哪位高手能解释下

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 12:47:29771browse

关于引用, 这种情况谁能解释下
代码段1:

<br />
$array_a		= array( 0 => 'a' );<br />
$array_b		= &$array_a;<br />
$array_c		= &$array_b;<br />
$array_d		= $array_b;<br />
<br />
$array_c[0]	= 'b';<br />
<br />
echo '变量a:';var_dump( $array_a );<br />
echo '变量b:';var_dump( $array_b );<br />
echo '变量c:';var_dump( $array_c );<br />
echo '变量d:';var_dump( $array_d );<br />

输出结果:
<br />
变量a:<br />
array (size=1)<br />
  0 => string 'b' (length=1)<br />
变量b:<br />
array (size=1)<br />
  0 => string 'b' (length=1)<br />
变量c:<br />
array (size=1)<br />
  0 => string 'b' (length=1)<br />
变量d:<br />
array (size=1)<br />
  0 => string 'a' (length=1)<br />

这里变量d值没变, 很好理解, 但改变一下变量c的引用位置, 如下:
代码段2:
<br />
$array_a		= array( 0 => 'a' );<br />
$array_b		= &$array_a;<br />
$array_c		= &$array_b[0];<br />
$array_d		= $array_b;<br />
<br />
$array_c	= 'b';<br />
<br />
echo '变量a:';var_dump( $array_a );<br />
echo '变量b:';var_dump( $array_b );<br />
echo '变量c:';var_dump( $array_c );<br />
echo '变量d:';var_dump( $array_d );<br />

输出结果:
<br />
变量a:<br />
array (size=1)<br />
  0 => &string 'b' (length=1)<br />
变量b:<br />
array (size=1)<br />
  0 => &string 'b' (length=1)<br />
变量c:<br />
string 'b' (length=1)<br />
变量d:<br />
array (size=1)<br />
  0 => &string 'b' (length=1)<br />

这里变量d也变了, 而且还变成了引用, 不理解为什么
求大神指教

引用
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn