Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP variables: references or copies
    primarykey
    data
    text
    <p>I'm confused about how PHP variable references work. In the examples below, I want to be able to access the string <em>hello</em> either as <code>$bar[0]</code> or <code>$barstack[0][0]</code>. It would seem that passing the array by reference in step 1 should be sufficient.</p> <p>The second example does not work. <code>$foostack[0]0]</code> is the string <em>hello</em>, but <code>$foo[0]</code> doesn't exist. At some point, the first element of $foostack becomes a copy of $foo, instead of a reference.</p> <p>The problem lies in the first line of step 2: When I push a reference on, I expect to pop a reference off. But <code>array_pop</code> returns a copy instead.</p> <p>Others have told me that if I have to worry about references and copies, then PHP is not the right language for me. That might be the best answer I'm going to get.</p> <p>FWIW, in order for <code>var_dump</code> to be useful, it needs to display some property that distinguishes between a reference and a copy. It does not. Maybe there's another function?</p> <blockquote> <p>My first PHP project seems to be going badly. Can someone help shed some light on the problems with this code?</p> </blockquote> <pre><code>&lt;?php echo "// This works!\n&lt;br /&gt;" ; // step 1 $bar = array() ; $barstack = array( &amp;$bar ) ; // step 2 array_push( $barstack[0], 'hello' ) ; // results echo count( $barstack[0] ) .';' .count( $bar ) ; echo "\n&lt;br /&gt;// This doesn't :(\n&lt;br /&gt;" ; // step 1 $foo = array() ; $foostack = array( &amp;$foo ) ; // step 2 $last = array_pop( $foostack ) ; array_push( $last, 'hello' ) ; array_push( $foostack, &amp;$last ) ; // results echo count( $foostack[0] ) .';' .count( $foo ) ; echo "\n&lt;br /&gt;// Version:\n&lt;br /&gt;" ; echo phpversion() ."\n" ; ?&gt; </code></pre> <blockquote> <p>The results can be viewed at the following URL:</p> <p><a href="http://www.gostorageone.com/tqis/hi.php" rel="nofollow">http://www.gostorageone.com/tqis/hi.php</a></p> <p>Version is 4.3.10. Upgrading the server is not practical.</p> <p>Desired outcomes:</p> <ol> <li>Explain the obvious if I've overlooked it</li> <li>Is this a bug? Any workarounds?</li> </ol> </blockquote> <p>Thanks!</p> <p>-Jim</p>
    singulars
    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.
 

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