Clarification on ZVals

Posted by Beachhouse on Stack Overflow See other posts from Stack Overflow or by Beachhouse
Published on 2012-12-18T16:44:24Z Indexed on 2012/12/18 17:03 UTC
Read the original article Hit count: 142

Filed under:

I was reading this: http://www.dereleased.com/2011/04/27/the-importance-of-zvals-and-circular-references/

And there's an example that lost me a bit.

$foo = &$bar;
$bar = &$foo;
$baz = 'baz';

$foo = &$baz;

var_dump($foo, $bar);
/*
string(3) "baz"
NULL
*/

If you’ve been following along, this should make perfect sense. $foo is created, and pointed at a ZVal location identified by $bar; when $bar is created, it points at the same place $foo was pointed. That location, of course, is null. When $foo is reassigned, the only thing that changes is to which ZVal $foo points; if we had assigned a different value to $foo first, then $bar would still retain that value.

I learned to program in C. I understand that PHP is different and it uses ZVals instead of memory locations as references. But when you run this code:

$foo = &$bar;
$bar = &$foo;

It seems to me that there would be two ZVals. In C there would be two memory locations (and the values would be of the opposite memory location).

Can someone explain?

© Stack Overflow or respective owner

Related posts about php