PHP references when used in recursive arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
teppyogi
Forum Newbie
Posts: 2
Joined: Thu Apr 21, 2005 9:01 am

PHP references when used in recursive arrays

Post by teppyogi »

hi,

does anyone have a good, sensible and logical explanation for the way php manages references in recursive arrays ?

here is a bit of code adapted from the PHP manual :

Code: Select all

<html><body><pre>
<?php

$top = array (
 'A' => array (),
 'B' => array ()
);

$top ї 'A' ] ї 'parent' ] = &$top;
$top ї 'B' ] ї 'parent' ] = &$top;

echo &quote;\n\n*******A************\n\n&quote;;
print_r ( $top ї 'A' ] ї 'parent' ] ); // output: Array ( їA] => Array ( їparent] => Array *RECURSION* ) їB] => Array () )
echo &quote;\n\n*******B************\n\n&quote;;
print_r ( $top ї 'B' ] ї 'parent' ] ); // output: Array ( їA] => Array ( їparent] => Array ( їA] => Array *RECURSION* їB] => Array () ) ) їB] => Array ( їparent] => Array *RECURSION* ) )

?>
</pre></body></html>
here is the output of the code above, what i don't understand is why the contents of $top [ 'A' ] [ 'parent' ] and $top [ 'B' ] [ 'parent' ] are not the same :

Code: Select all

*******A************

Array
(
    їA] => Array
        (
            їparent] => Array
 *RECURSION*
        )

    їB] => Array
        (
        )

)


*******B************

Array
(
    їA] => Array
        (
            їparent] => Array
                (
                    їA] => Array
 *RECURSION*
                    їB] => Array
                        (
                        )

                )

        )

    їB] => Array
        (
            їparent] => Array
 *RECURSION*
        )

)
thanks for your help,

tepp.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Whooaa... even thinking about the logic in this is making my head spin :P

I'm gonna play with this cos it looks like crazy fun :-D
R0d Longfella
Forum Newbie
Posts: 20
Joined: Fri Apr 08, 2005 7:17 am

Post by R0d Longfella »

PHP uses a symbolic reference table to keep track of variables. If you're really interested in this, you should read up on all what ZEND has to say about variables.

As I mentioned before, PHP keeps track of variables with a symbolic reference table. If you create a variable, the actual content of that variable is stored somewhere in memory. And that content is then being referenced by a symbol. (ie. $var is a symbol, and "this" is the contents) Now the magic happens when you make a reference to another symbol. (the PHP manual calls this "Making a reference to a variable") You can do this using "$var2 = &$var". Now the contents of $var is not duplicated, yet another reference is added to the contents. The piece of memory containing "this" now has two references. And in the symbolic reference table there are two entry ($var, and $var2) which both point to the same content.

I hope this explains the use of references a bit. If you have any other question, feel free.

R0d
teppyogi
Forum Newbie
Posts: 2
Joined: Thu Apr 21, 2005 9:01 am

Post by teppyogi »

thanks for your help.

i've actually read a fair deal on php references and i've gotten to understand most of the little peculiarities, however the code above still escapes my understanding. as you'll see, even using the symbolic reference table, there is no apparent reason why the same reference applied twice in a row will lead to different variables. what the manual has to say about this in chapter 20 is "Complex arrays are sometimes rather copied than referenced", but it doesn't give the exact nature of the complexity nor when the "sometimes" actually occurs.

so you're still on ;-)

thanks,

tepp.
Post Reply