Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6666819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:52:07+00:00 2026-05-26T02:52:07+00:00

I’m confused about how PHP variable references work. In the examples below, I want

  • 0

I’m confused about how PHP variable references work. In the examples below, I want to be able to access the string hello either as $bar[0] or $barstack[0][0]. It would seem that passing the array by reference in step 1 should be sufficient.

The second example does not work. $foostack[0]0] is the string hello, but $foo[0] doesn’t exist. At some point, the first element of $foostack becomes a copy of $foo, instead of a reference.

The problem lies in the first line of step 2: When I push a reference on, I expect to pop a reference off. But array_pop returns a copy instead.

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.

FWIW, in order for var_dump 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?

My first PHP project seems to be going badly. Can someone help shed some light on the
problems with this code?

<?php
echo "// This works!\n<br />" ;

// step 1
$bar = array() ;
$barstack = array( &$bar ) ;

// step 2
array_push( $barstack[0], 'hello' ) ;

// results
echo count( $barstack[0] ) .';' .count( $bar ) ;


echo "\n<br />// This doesn't :(\n<br />" ;

// step 1
$foo = array() ;
$foostack = array( &$foo ) ;

// step 2
$last = array_pop( $foostack ) ;
array_push( $last, 'hello' ) ;
array_push( $foostack, &$last ) ;

// results
echo count( $foostack[0] ) .';' .count( $foo ) ;


echo "\n<br />// Version:\n<br />" ;
echo phpversion() ."\n" ;
?>

The results can be viewed at the following URL:

http://www.gostorageone.com/tqis/hi.php

Version is 4.3.10. Upgrading the server is not practical.

Desired outcomes:

  1. Explain the obvious if I’ve overlooked it
  2. Is this a bug? Any workarounds?

Thanks!

-Jim

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:52:07+00:00Added an answer on May 26, 2026 at 2:52 am

    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.

    Let’s go through the example which does not work in your eyes, just looking what actually happens:

    // step 1
    $foo = array(); # 1.
    $foostack = array( &$foo ); # 2.
    
    • 1.: You initialize the variable $foo to an empty array.
    • 2.: You initialize the variable $foostack to an array and the first element of the array is an alias of the variable $foo. This is exactly the same as writing: $foostack[] =& $foo;

    On to the next step:

    // step 2
    $last = array_pop($foostack); # 3.
    array_push($last, 'hello'); # 4.
    array_push($foostack, &$last); # 5.
    
    • 3.: You assign the last element’s value of the array $foostack to $last and you remove the last element from the array $foostack. Note: array_pop returns a value, not a reference.
    • 4.: You add 'hello' as a new element to an empty array in $last.
    • 5.: You add &$last as a new element to $foostack;

    So which variables do we have now?

    • First of all $foo which just contains an empty array. The last element of $foostack was once reference to it (2.), but you have removed that directly after (3.). As $foo and it’s value has not been changed any longer, it’s just an empty array array().
    • Then there is $last, 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 'hello' as first element to it. $last is an array with one string element in there.
    • Then there is $foostack. It’s an array that get’s a reference to $foo in (2.), then that reference is removed in (3.). Finally an alias to $last is added to it.

    This is exactly what the rest of your code outputs:

    echo count($foostack[0]) .';'. count($foo);
    

    $foostack[0] is the alias to $last – the array with the string 'hello' as only element, while $foo is just $foo, the empty array array().

    It makes no difference if you execute that with PHP 4 or 5.


    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 array_pop_ref():

    // step 1
    $foo = array();
    $foostack = array( &$foo );
    
    // step 2
    $last =& array_pop_ref($foostack);
    array_push($last, 'hello');
    array_push($foostack, &$last);
    
    // results
    echo count($foostack[0]) .';' .count($foo); # 1;1
    

    The array_pop_ref function:

    function &array_pop_ref(&$array)
    {
        $result = NULL;
        if (!is_array($array)) return $result;
        $keys = array_keys($array);
        $end = end($keys);
        if (false === $end) return $result;
        $result =& $array[$end];
        array_pop($array);
        return $result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
Does anyone know how can I replace this 2 symbol below from the string
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.