Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First time through the loop</p> <pre><code>foreach($arr as $k=&gt;$v){ // Sets $v to a value of "one" $v =&amp; $arr[$k]; // Sets $v as a reference to $arr[1] ("one") $v .= $k; // Sets $v (and hence also $arr[1]) to "one1" </code></pre> <p>Second time through the loop</p> <pre><code>foreach($arr as $k=&gt;$v){ // Sets $v to a value of "two"... // because $v is already set as a reference to $arr[1] from the previous loop, // this changes $arr[1] to a value of "two" $v =&amp; $arr[$k]; // Sets $v as a reference to $arr[2] ("two") // It no longer references $arr[1] so $arr[1] will not be changed any further $v .= $k; // Sets $v (and hence also $arr[2]) to "two2" </code></pre> <p>Third time through the loop</p> <pre><code>foreach($arr as $k=&gt;$v){ // Sets $v to a value of "three"... // because $v is already set as a reference to $arr[2] from the previous loop, // this changes $arr[2] to a value of "three" $v =&amp; $arr[$k]; // Sets $v as a reference to $arr[3] ("three") // It no longer references $arr[2] so $arr[2] will not be changed any further $v .= $k; // Sets $v (and hence also $arr[3]) to "three3" </code></pre> <p><strong>If you use the unset()</strong></p> <p>First time through the loop</p> <pre><code>foreach($arr as $k=&gt;$v){ // Sets $v to a value of "one" $v =&amp; $arr[$k]; // Sets $v as a reference to $arr[1] ("one") $v .= $k; // Sets $v (and hence also $arr[1]) to "one1" unset($v); // Unsets $v as a reference, it no longer points to $arr[1] </code></pre> <p>Second time through the loop</p> <pre><code>foreach($arr as $k=&gt;$v){ // Sets $v to a value of "two"... // As $v is no longer set as a reference to $arr[1], // this leaves $arr[1] unchanged by this loop $v =&amp; $arr[$k]; // Sets $v as a reference to $arr[2] ("two") $v .= $k; // Sets $v (and hence also $arr[2]) to "two2" unset($v); // Unsets $v as a reference, it no longer points to $arr[2] </code></pre> <p>Third time through the loop</p> <pre><code>foreach($arr as $k=&gt;$v){ // Sets $v to a value of "three"... // As $v is no longer set as a reference to $arr[2], // this leaves $arr[2] unchanged by this loop $v =&amp; $arr[$k]; // Sets $v as a reference to $arr[3] ("three") $v .= $k; // Sets $v (and hence also $arr[3]) to "three3" unset($v); // Unsets $v as a reference, it no longer points to $arr[3] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload