Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had to spend a few hours to figure out why a[3] is changing on each iteration. This is the explanation at which I arrived. </p> <p>There are two types of variables in PHP: normal variables and reference variables. If we assign a reference of a variable to another variable, the variable becomes a reference variable. </p> <p>for example in</p> <pre><code>$a = array('zero', 'one', 'two', 'three'); </code></pre> <p>if we do </p> <pre><code>$v = &amp;$a[0] </code></pre> <p>the 0th element (<code>$a[0]</code>) becomes a reference variable. <code>$v</code> points towards that variable; therefore, if we make any change to <code>$v</code>, it will be reflected in <code>$a[0]</code> and vice versa. </p> <p>now if we do </p> <pre><code>$v = &amp;$a[1] </code></pre> <p><code>$a[1]</code> will become a reference variable and <code>$a[0]</code> will become a normal variable (Since no one else is pointing to <code>$a[0]</code> it is converted to a normal variable. PHP is smart enough to make it a normal variable when no one else is pointing towards it)</p> <p>This is what happens in the first loop </p> <pre><code>foreach ($a as &amp;$v) { } </code></pre> <p>After the last iteration <code>$a[3]</code> is a reference variable. </p> <p>Since <code>$v</code> is pointing to <code>$a[3]</code> any change to <code>$v</code> results in a change to <code>$a[3]</code></p> <p>in the second loop, </p> <pre><code>foreach ($a as $v) { echo $v.'-'.$a[3].PHP_EOL; } </code></pre> <p>in each iteration as <code>$v</code> changes, <code>$a[3]</code> changes. (because <code>$v</code> still points to <code>$a[3]</code>). This is the reason why <code>$a[3]</code> changes on each iteration.</p> <p>In the iteration before the last iteration, <code>$v</code> is assigned the value 'two'. Since <code>$v</code> points to <code>$a[3]</code>, <code>$a[3]</code> now gets the value 'two'. Keep this in mind. </p> <p>In the last iteration, <code>$v</code> (which points to <code>$a[3]</code>) now has the value of 'two', because <code>$a[3]</code> was set to two in the previous iteration. <code>two</code> is printed. This explains why 'two' is repeated when $v is printed in the last iteration. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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