全面解读php-引用变量(&)
<h1>本文讲述引用传值的核心原理,看完即可扫清一切和引用传值相关的内容,不会了记得画图。</h1><h1>一、memory_get_usage的使用</h1>
<h2>传值赋值</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义一个变量</span>
<span style="color: rgba(128, 0, 128, 1)">$a</span> = <span style="color: rgba(0, 128, 128, 1)">range</span>(0, 10000<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">memory_get_usage() 可以查看PHP内存使用量</span>
<span style="color: rgba(0, 128, 128, 1)">var_dump</span>(memory_get_usage()); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> int(989778)
// 定义变量b,将a变量的值赋值给b</span>
<span style="color: rgba(128, 0, 128, 1)">$b</span> = <span style="color: rgba(128, 0, 128, 1)">$a</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">var_dump</span>(memory_get_usage()); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> int(989764)
// 对a进行修改,只有发生了COW机制的才会重新开辟一块儿存储空间
// COW机制: Copy-On-Write </span>
<span style="color: rgba(128, 0, 128, 1)">$a</span> = <span style="color: rgba(0, 128, 128, 1)">range</span>(0, 10000<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">var_dump</span>(memory_get_usage());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">int(1855643)</span></pre>
</div>
<p><span style="font-size: 16px"><strong>原理说明:</strong></span></p>
<p><span style="font-size: 16px"><strong>1、定义一个变量<span style="color: rgba(128, 0, 128, 1)">$a=range(0,1000);</span></strong></span></p>
<h4><strong><code>;<img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620201806026-1166110475.png"></code></strong></h4>
<h4><strong><code><span style="color: rgba(128, 0, 128, 1)">2、</span><span style="color: rgba(128, 0, 128, 1)">$b = $a;</span></code></strong></h4>
<p><strong><img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620202140980-1717451014.png"></strong></p>
<h4><strong>3、对a进行修改 <span style="color: rgba(128, 0, 128, 1)"><code>$a = range(0, 10000)</code></span></strong></h4>
<p><strong><img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620202455571-1848557532.png"></strong></p>
<p><span style="font-size: 18px"><strong><span style="color: rgba(255, 0, 0, 1)">COW机制</span></strong></span></p>
<p><strong>PHP写时复制机制(Copy-on-Write,也缩写为COW)</strong></p>
<ul>
<li>顾名思义,就是在写入时才真正复制一份内存进行修改。</li>
<li>COW最早应用在Unix系统中对线程与内存使用的优化,后面广泛的被使用在各种编程语言中,如C++的STL等。 </li>
<li>在PHP内核中,COW也是主要的内存优化手段。</li>
<li>在通过变量赋值的方式赋值给变量时,不会申请新内存来存放新变量的值,而是简单的通过一个计数器来共用内存。只有在其中的一个引用指向变量的值发生变化时,才申请新空间来保存值内容,以减少对内存的占用。</li>
<li>在很多场景下PHP都使用COW进行内存的优化。比如:变量的多次赋值、函数参数传递,并在函数体内修改实参等。</li>
</ul>
<h3 id="articleHeader2"><span style="font-size: 14pt">引用赋值</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义一个变量</span>
<span style="color: rgba(128, 0, 128, 1)">$a</span> = <span style="color: rgba(0, 128, 128, 1)">range</span>(0, 10000<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">var_dump</span>(memory_get_usage()); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">int(989761)
// 定义变量b,将a变量的引用赋给b</span>
<span style="color: rgba(128, 0, 128, 1)">$b</span> = &<span style="color: rgba(128, 0, 128, 1)">$a</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">var_dump</span>(memory_get_usage()); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">int(989876)
// 对a进行修改</span>
<span style="color: rgba(128, 0, 128, 1)">$a</span> = <span style="color: rgba(0, 128, 128, 1)">range</span>(0, 10000<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">var_dump</span>(memory_get_usage()); int(989869)</pre>
</div>
<h2><span style="font-size: 18px"><strong>原理说明:</strong></span></h2>
<ol>
<li>
<h4><strong>定义一个变量 <code>$a = range(0, 10000);</code></strong></h4>
<h4><strong><code><img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620201806026-1166110475.png"></code></strong></h4>
</li>
<li>
<h4><strong>定义变量b,将a变量的引用赋给b <code>$b = &$a;</code></strong></h4>
<h4><img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620223111996-580501078.png"></h4>
</li>
<li>
<h4><strong>对a进行修改 <code>$a = range(0, 10000);</code></strong></h4>
</li>
</ol>
<p> <img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620223201004-981938939.png"></p>
<h1>二、xdebug_debug_zval() 用于显示变量的信息</h1>
<h2 id="articleHeader4">传值赋值</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$a</span> = 1<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">xdebug_debug_zval() 用于显示变量的信息。需要安装xdebug扩展。</span>
xdebug_debug_zval('a'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a:(refcount=1, is_ref=0)
// 定义变量b,把a的值赋值给b</span>
<span style="color: rgba(128, 0, 128, 1)">$b</span> = <span style="color: rgba(128, 0, 128, 1)">$a</span><span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'a');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a: (refcount=2, is_ref=0)</span>
xdebug_debug_zval('b');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">b: (refcount=2, is_ref=0)
// a进行写操作</span>
<span style="color: rgba(128, 0, 128, 1)">$a</span> = 2<span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'a');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a: (refcount=1, is_ref=0)</span>
xdebug_debug_zval('b');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">b: (refcount=1, is_ref=0)</span></pre>
</div>
<h4><strong>1、定义变量 <code>$a = 1;</code></strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$a</span> = 1<span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'a'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a:(refcount=1, is_ref=0)</span></pre>
</div>
<p><code>refcount=1</code> 表示该变量指向的内存地址的引用个数变为1<br><code>is_ref=0</code> 表示该变量不是引用</p>
<p><img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620224407865-1665309735.png"></p>
<h4><strong>2、定义变量 <code>$b</code> ,把 <code>$a</code> 的值赋给 <code>$b</code>, <code>$b = $a;</code></strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义变量b,把a的值赋值给b</span>
<span style="color: rgba(128, 0, 128, 1)">$b</span> = <span style="color: rgba(128, 0, 128, 1)">$a</span><span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'a');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a: (refcount=2, is_ref=0)</span>
xdebug_debug_zval('b');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">b: (refcount=2, is_ref=0)</span></pre>
</div>
<p><code>refcount=2</code> 表示该变量指向的内存地址的引用个数变为2<br><code>is_ref=0</code> 表示该变量不是引用</p>
<p><img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620224457320-1319121191.png"></p>
<h4><strong>3、对变量 <code>$a</code> 进行写操作 <code>$a = 2;</code></strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> a进行写操作</span>
<span style="color: rgba(128, 0, 128, 1)">$a</span> = 2<span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'a');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a: (refcount=1, is_ref=0)</span>
xdebug_debug_zval('b');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">b: (refcount=1, is_ref=0)</span></pre>
</div>
<pre class="hljs perl"><code><img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620224601995-1136083960.png"></code></pre>
<p>因为COW机制,对变量 <code>$a</code> 进行写操作时,会为变量 <code>$a</code> 新分配一块内存空间,用于存储变量 <code>$a</code> 的值。<br>此时 <code>$a</code> 和 <code>$b</code> 指向的内存地址的引用个数都变为1。</p>
<h2 id="articleHeader5">引用赋值</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$a</span> = 1<span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'a');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a: (refcount=1, is_ref=0)
// 定义变量b,把a的引用赋给b</span>
<span style="color: rgba(128, 0, 128, 1)">$b</span> = &<span style="color: rgba(128, 0, 128, 1)">$a</span><span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'a');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a: (refcount=2, is_ref=1)</span>
xdebug_debug_zval('b');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">b: (refcount=2, is_ref=1
// a进行写操作</span>
<span style="color: rgba(128, 0, 128, 1)">$a</span> = 2<span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'a');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a: (refcount=2, is_ref=1)</span>
xdebug_debug_zval('b');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">b: (refcount=2, is_ref=1)</span></pre>
</div>
<h3><strong>原理说明:</strong></h3>
<h4><strong>定义变量 <code>$a = 1;</code></strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$a</span> = 1<span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'a');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a: (refcount=1, is_ref=0)</span> </pre>
</div>
<p><code>refcount=1</code> 表示该变量指向的内存地址的引用个数变为1<br><code>is_ref=0</code> 表示该变量不是引用</p>
<p><img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620224924205-286811045.png"></p>
<h4><strong>2、定义变量 <code>$b</code> ,把 <code>$a</code> 的引用赋给 <code>$b</code>, <code>$b = &$a;</code></strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义变量b,把a的引用赋给b</span>
<span style="color: rgba(128, 0, 128, 1)">$b</span> = &<span style="color: rgba(128, 0, 128, 1)">$a</span><span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'a');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a: (refcount=2, is_ref=1)</span>
xdebug_debug_zval('b');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">b: (refcount=2, is_ref=1</span> </pre>
</div>
<p><code>refcount=2</code> 表示该变量指向的内存地址的引用个数变为2<br><code>is_ref=1</code> 表示该变量是引用</p>
<p><img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620225023574-831266811.png"></p>
<h4><strong>3、对变量 <code>$a</code> 进行写操作 <code>$a = 2;</code></strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> a进行写操作</span>
<span style="color: rgba(128, 0, 128, 1)">$a</span> = 2<span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'a');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a: (refcount=2, is_ref=1)</span>
xdebug_debug_zval('b');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">b: (refcount=2, is_ref=1)</span></pre>
</div>
<p> <img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620225042760-164173208.png"></p>
<ul>
<li>因为变量 <code>$a</code> 和变量 <code>$b</code> 指向相同的内存地址,其实引用。</li>
<li>对变量 <code>$a</code> 进行写操作时,会直接修改指向的内存空间的值,因此变量 <code>$b</code> 的值会跟着一起改变。</li>
</ul>
<h2 id="articleHeader6">三、当变量时引用时,unset()只会取消引用,不会销毁内存空间</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$a</span> = 1<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$b</span> = &<span style="color: rgba(128, 0, 128, 1)">$a</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> unset 只会取消引用,不会销毁内存空间</span>
<span style="color: rgba(0, 0, 255, 1)">unset</span>(<span style="color: rgba(128, 0, 128, 1)">$b</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 128, 1)">$a</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1</span></pre>
</div>
<h3><strong>原理说明:</strong></h3>
<h4><strong>定义变量 <code>$a</code> ,并将 <code>$a</code> 的引用赋给变量 <code>$b</code></strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$a</span> = 1<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$b</span> = &<span style="color: rgba(128, 0, 128, 1)">$a</span>;</pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620225244089-394455557.png"></p>
<h4><strong>销毁 <code>$b</code></strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">unset</span>(<span style="color: rgba(128, 0, 128, 1)">$b</span>);</pre>
</div>
<p> </p>
<p><img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620225313932-1267859272.png"></p>
<h4><strong>输出 <code>$a</code></strong></h4>
<p>虽然销毁的 <code>$b</code>,但是 <code>$a</code> 的引用和内存空间依旧存在。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 128, 1)">$a</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1</span></pre>
</div>
<h1 id="articleHeader7">四、php中对象本身就是引用赋值</h1>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Person
{
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(128, 0, 128, 1)">$name</span> = 'zhangsan'<span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(128, 0, 128, 1)">$p1</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Person;
xdebug_debug_zval(</span>'p1'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">p1: (refcount=1, is_ref=0)=class Person { public $name = (refcount=2, is_ref=0)=1 }</span>
<span style="color: rgba(128, 0, 128, 1)">$p2</span> = <span style="color: rgba(128, 0, 128, 1)">$p1</span><span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'p1');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> p1: (refcount=2, is_ref=0)=class Person { public $name= (refcount=2, is_ref=0)=1 }</span>
xdebug_debug_zval('p2'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">p2: (refcount=2, is_ref=0)=class Person { public $name= (refcount=2, is_ref=0)=1 }</span>
<span style="color: rgba(128, 0, 128, 1)">$p2</span>->age = 2<span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'p1');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">p1: (refcount=2, is_ref=0)=class Person { public $name= (refcount=1, is_ref=0)=2 }</span>
xdebug_debug_zval('p2');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">p2: (refcount=2, is_ref=0)=class Person { public $name= (refcount=1, is_ref=0)=2 }</span></pre>
</div>
<h3>原理说明</h3>
<h4><strong>1、实例化对象 <code>$p1 = new Person;</code></strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$p1</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Person;
xdebug_debug_zval(</span>'p1'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">p1: (refcount=1, is_ref=0)=class Person { public $name = (refcount=2, is_ref=0)=1 }</span> </pre>
</div>
<p><code>refcount=1</code> 表示该变量指向的内存地址的引用个数变为1<br><code>is_ref=0</code> 表示该变量不是引用</p>
<h4><strong>2、把 <code>$p1</code> 赋给 <code>$p2</code></strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$p2</span> = <span style="color: rgba(128, 0, 128, 1)">$p1</span><span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'p1');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> p1: (refcount=2, is_ref=0)=class Person { public $name= (refcount=2, is_ref=0)=1 }</span>
xdebug_debug_zval('p2'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">p2: (refcount=2, is_ref=0)=class Person { public $name= (refcount=2, is_ref=0)=1 }</span> </pre>
</div>
<p><code>refcount=2</code> 表示该变量指向的内存地址的引用个数变为2</p>
<p><img src="https://img2018.cnblogs.com/blog/1064872/201906/1064872-20190620225716565-1793016897.png"></p>
<h4><strong>3、对 <code>$p2</code> 中的属性 <span style="font-family: monospace">name </span>进行写操作</strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$p2</span>->name = 'lisi'<span style="color: rgba(0, 0, 0, 1)">;
xdebug_debug_zval(</span>'p1');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">p1: (refcount=2, is_ref=0)=class Person { public $name= (refcount=1, is_ref=0)=2 }</span>
xdebug_debug_zval('p2');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">p2: (refcount=2, is_ref=0)=class Person { public $name= (refcount=1, is_ref=0)=2 }</span> </pre>
</div>
<p>因为php中对象本身就是引用赋值。对 <code>$p2</code> 中的属性 <span style="font-family: monospace">name</span> 进行写操作时,会直接修改指向的内存空间的值,因此变量 <code>$p1</code>的 <span style="font-family: monospace">name </span>属性的值会跟着一起改变。</p>
<h1 id="articleHeader8">五、实战例题分析</h1>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">写出如下程序的输出结果
</span><span style="color: rgba(128, 0, 128, 1)">$d</span> = ['a', 'b', 'c'<span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span>(<span style="color: rgba(128, 0, 128, 1)">$d</span> <span style="color: rgba(0, 0, 255, 1)">as</span> <span style="color: rgba(128, 0, 128, 1)">$k</span> => <span style="color: rgba(128, 0, 128, 1)">$v</span><span style="color: rgba(0, 0, 0, 1)">)
{
</span><span style="color: rgba(128, 0, 128, 1)">$v</span> = &<span style="color: rgba(128, 0, 128, 1)">$d</span>[<span style="color: rgba(128, 0, 128, 1)">$k</span><span style="color: rgba(0, 0, 0, 1)">];
}
程序运行时,每一次循环结束后变量 </span><span style="color: rgba(128, 0, 128, 1)">$d</span><span style="color: rgba(0, 0, 0, 1)"> 的值是什么?请解释。
程序执行完成后,变量 </span><span style="color: rgba(128, 0, 128, 1)">$d</span> 的值是什么?请解释。</pre>
</div>
<h3 id="articleHeader9">1. 第一次循环</h3>
<h4><strong>推算出进入 <code>foreach</code> 时 <code>$v</code>、<code>$d[$k]</code> 的值</strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$k</span> = 0
<span style="color: rgba(128, 0, 128, 1)">$v</span> = 'a'
<span style="color: rgba(128, 0, 128, 1)">$d</span>[<span style="color: rgba(128, 0, 128, 1)">$k</span>] = <span style="color: rgba(128, 0, 128, 1)">$d</span> = 'a'</pre>
</div>
<p>此时,<code>$v</code> 和 <code>$d</code> 在内存中分别开辟了一块空间</p>
<p>$v 和 $d 在内存中分别开辟了一块空间</p>
<h4><strong><code>$v = &$d</code> 改变了 $v 指向的内存地址</strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$v</span> = &<span style="color: rgba(128, 0, 128, 1)">$d</span>
</span><span style="color: rgba(128, 0, 128, 1)">$v</span> = &<span style="color: rgba(128, 0, 128, 1)">$d</span> 改变了 <span style="color: rgba(128, 0, 128, 1)">$val</span> 指向的内存地址</pre>
</div>
<h4><strong>第一次循环后 $d 的值:</strong></h4>
<div class="cnblogs_code">
<pre>['a', 'b', 'c']</pre>
</div>
<h3 id="articleHeader10">2. 第二次循环</h3>
<h4><strong>进入 <code>foreach</code> 时 <code>$v</code> 被赋值为 'b',此时<code>$v</code>指向的内存地址与 <code>$d</code> 相同,且为引用,因此 <code>$d</code> 的值被修改为 'b'</strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$v</span> = 'b' => <span style="color: rgba(128, 0, 128, 1)">$d</span> = 'b'
<span style="color: rgba(128, 0, 128, 1)">$v</span> = ‘b’ => <span style="color: rgba(128, 0, 128, 1)">$d</span> = ‘b’</pre>
</div>
<h4><strong>推算出进入 <code>foreach</code> 时 <code>$d[$k]</code> 的值</strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$k</span> = 1
<span style="color: rgba(128, 0, 128, 1)">$d</span>[<span style="color: rgba(128, 0, 128, 1)">$k</span>] = <span style="color: rgba(128, 0, 128, 1)">$d</span> = 'b'
<span style="color: rgba(128, 0, 128, 1)">$d</span> = ‘b’</pre>
</div>
<h4><strong><code>$v = &$d</code> 改变了 $v 指向的内存地址</strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$v</span> = &<span style="color: rgba(128, 0, 128, 1)">$d</span>
</span><span style="color: rgba(128, 0, 128, 1)">$v</span> = &<span style="color: rgba(128, 0, 128, 1)">$d</span></pre>
</div>
<h4><strong>第二次循环后 <code>$d</code> 的值</strong></h4>
<div class="cnblogs_code">
<pre>['b', 'b', 'c']</pre>
</div>
<h3 id="articleHeader11">3. 第三次循环</h3>
<h4><strong>进入 <code>foreach</code> 时 <code>$v</code> 被赋值为 'c',此时<code>$v</code>指向的内存地址与 <code>$d</code> 相同,且为引用,因此 <code>$d</code> 的值被修改为 'c'</strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$v</span> = 'c' => <span style="color: rgba(128, 0, 128, 1)">$d</span> = 'c'
<span style="color: rgba(128, 0, 128, 1)">$v</span> = ‘c’ => <span style="color: rgba(128, 0, 128, 1)">$d</span> = ‘c’]</pre>
</div>
<h4><strong>推算出进入 <code>foreach</code> 时 <code>$d[$k]</code> 的值</strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$k</span> = 2
<span style="color: rgba(128, 0, 128, 1)">$d</span> = 'c'
<span style="color: rgba(128, 0, 128, 1)">$d</span> = ‘c’</pre>
</div>
<h4><strong><code>$v = &$d</code> 改变了 $v 指向的内存地址</strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$v</span> = &<span style="color: rgba(128, 0, 128, 1)">$d</span>
</span><span style="color: rgba(128, 0, 128, 1)">$v</span> = &<span style="color: rgba(128, 0, 128, 1)">$d</span></pre>
</div>
<h4><strong>第三次循环后 <code>$d</code> 的值</strong></h4>
<div class="cnblogs_code">
<pre>['b', 'c', 'c']</pre>
</div>
<h3 id="articleHeader12">4. 实测</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$d</span> = ['a', 'b', 'c'<span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(128, 0, 128, 1)">$d</span> <span style="color: rgba(0, 0, 255, 1)">as</span> <span style="color: rgba(128, 0, 128, 1)">$k</span>=><span style="color: rgba(128, 0, 128, 1)">$v</span><span style="color: rgba(0, 0, 0, 1)">)
{
</span><span style="color: rgba(128, 0, 128, 1)">$v</span> = &<span style="color: rgba(128, 0, 128, 1)">$d</span>[<span style="color: rgba(128, 0, 128, 1)">$k</span><span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(0, 128, 128, 1)">print_r</span>(<span style="color: rgba(128, 0, 128, 1)">$d</span><span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 128, 128, 1)">print_r</span>(<span style="color: rgba(128, 0, 128, 1)">$d</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">Array</span><span style="color: rgba(0, 0, 0, 1)">
(
[</span>0] =><span style="color: rgba(0, 0, 0, 1)"> a
[</span>1] =><span style="color: rgba(0, 0, 0, 1)"> b
[</span>2] =><span style="color: rgba(0, 0, 0, 1)"> c
)
</span><span style="color: rgba(0, 0, 255, 1)">Array</span><span style="color: rgba(0, 0, 0, 1)">
(
[</span>0] =><span style="color: rgba(0, 0, 0, 1)"> b
[</span>1] =><span style="color: rgba(0, 0, 0, 1)"> b
[</span>2] =><span style="color: rgba(0, 0, 0, 1)"> c
)
</span><span style="color: rgba(0, 0, 255, 1)">Array</span><span style="color: rgba(0, 0, 0, 1)">
(
[</span>0] =><span style="color: rgba(0, 0, 0, 1)"> b
[</span>1] =><span style="color: rgba(0, 0, 0, 1)"> c
[</span>2] =><span style="color: rgba(0, 0, 0, 1)"> c
)
</span><span style="color: rgba(0, 0, 255, 1)">Array</span><span style="color: rgba(0, 0, 0, 1)">
(
[</span>0] =><span style="color: rgba(0, 0, 0, 1)"> b
[</span>1] =><span style="color: rgba(0, 0, 0, 1)"> c
[</span>2] =><span style="color: rgba(0, 0, 0, 1)"> c
)</span></pre>
</div>
<p> </p>
<p> </p>
<h4><strong>`</strong></h4>
<div class="widget-codetool"> </div><br><br>
来源:https://www.cnblogs.com/chrdai/p/11061174.html
頁:
[1]