Sunday, August 7, 2011

PHP copy on write - how PHP manages variable memory (2)

Continue with my last post http://hengrui-li.blogspot.com/2011/08/php-copy-on-write-how-php-manages.html, let's check some interesting cases.

case 1:

$name   = 'henry';
$fname = 'henry';

How does PHP handle these two variables?

//the output is: name: (refcount=1, is_ref=0)='henry'
xdebug_debug_zval('name');

// the output is:  fname: (refcount=1, is_ref=0)='henry'
xdebug_debug_zval('fname');

From the output, we can see that PHP actually creates two zval to store each of them.

case 2:

$fname = $name = 'henry';

// the output is: name: (refcount=2, is_ref=0)='henry'
xdebug_debug_zval('name');

//the output is: fname: (refcount=2, is_ref=0)='henry'
xdebug_debug_zval('fname');

In this case, we find that PHP only uses one zval, which is more efficient.

No comments: