Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code works fine, there is no bug, and it is independent to PHP 4 or 5. Maybe it helps if this is simply explained to you.</p> <p>Let's go through the example which does not work in your eyes, just looking what actually happens:</p> <pre><code>// step 1 $foo = array(); # 1. $foostack = array( &amp;$foo ); # 2. </code></pre> <ul> <li>1.: You initialize the variable <code>$foo</code> to an empty array.</li> <li>2.: You initialize the variable <code>$foostack</code> to an array and the first element of the array is an alias of the variable <code>$foo</code>. This is exactly the same as writing: <code>$foostack[] =&amp; $foo;</code></li> </ul> <p>On to the next step:</p> <pre><code>// step 2 $last = array_pop($foostack); # 3. array_push($last, 'hello'); # 4. array_push($foostack, &amp;$last); # 5. </code></pre> <ul> <li>3.: You assign the last element's <em>value</em> of the array <code>$foostack</code> to <code>$last</code> and you remove the last element from the array <code>$foostack</code>. <strong><em>Note:</em></strong> <code>array_pop</code> returns a value, not a reference.</li> <li>4.: You add <code>'hello'</code> as a new element to an empty array in <code>$last</code>.</li> <li>5.: You add <code>&amp;$last</code> as a new element to <code>$foostack</code>;</li> </ul> <p>So which variables do we have now?</p> <ul> <li>First of all <code>$foo</code> which just contains an empty array. The last element of <code>$foostack</code> was once reference to it (2.), but you have removed that directly after (3.). As <code>$foo</code> and it's value has not been changed any longer, it's just an empty array <code>array()</code>.</li> <li>Then there is <code>$last</code>, which got an empty array in 3.. That's just an empty array, it's a value not a reference. In (4.) you add the string <code>'hello'</code> as first element to it. <code>$last</code> is an array with one string element in there.</li> <li>Then there is <code>$foostack</code>. It's an array that get's a reference to <code>$foo</code> in (2.), then that reference is removed in (3.). Finally an alias to <code>$last</code> is added to it.</li> </ul> <p>This is exactly what the rest of your code outputs:</p> <pre><code>echo count($foostack[0]) .';'. count($foo); </code></pre> <p><code>$foostack[0]</code> is the alias to <code>$last</code> - the array with the string <code>'hello'</code> as only element, while <code>$foo</code> is just <code>$foo</code>, the empty array <code>array()</code>.</p> <p>It makes no difference if you execute that with PHP 4 or 5.</p> <hr> <p>As you write that's "wrong", I assume you were just not able to achieve what you wanted. You're probably looking for a function that is able to return the reference to the last element of an array before removing it. Let's call that function <code>array_pop_ref()</code>:</p> <pre><code>// step 1 $foo = array(); $foostack = array( &amp;$foo ); // step 2 $last =&amp; array_pop_ref($foostack); array_push($last, 'hello'); array_push($foostack, &amp;$last); // results echo count($foostack[0]) .';' .count($foo); # 1;1 </code></pre> <p>The <code>array_pop_ref</code> function:</p> <pre><code>function &amp;array_pop_ref(&amp;$array) { $result = NULL; if (!is_array($array)) return $result; $keys = array_keys($array); $end = end($keys); if (false === $end) return $result; $result =&amp; $array[$end]; array_pop($array); return $result; } </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