PHP Function Reference

PHP debug_zval_dump() Function



The PHP debug_zval_dump() function dumps a string representation of an internal zval structure to output. This is mostly useful for understanding or debugging implementation details of the Zend Engine or PHP extensions.

Syntax

debug_zval_dump(value, values)

Parameters

value Required. Specify the variable or value to dump.
values Optional. Specify further variables or values to dump. Multiple parameters are allowed.

Return Value

No value is returned.

Example: debug_zval_dump() example

The example below shows the usage of debug_zval_dump() function.

<?php
$var1 = 'Hello';
$var1 .= ' World';
$var2 = $var1;
$var3 = "Hello World";

debug_zval_dump($var1);
debug_zval_dump($var2);
debug_zval_dump($var3);
?>

The output of the above code will be:

string(11) "Hello World" refcount(3)
string(11) "Hello World" refcount(3)
string(11) "Hello World" refcount(1)

Understanding the refcount

The Zend Engine uses reference counting for two different purposes:

  • Optimizing memory usage using a technique called "copy on write", where multiple variables holding the same value point to the same copy in memory. When any of the variables is modified, it is pointed to a new copy in memory, and the reference count on the original is decreased by 1.
  • Tracking variables which have been assigned or passed by reference. This refcount is stored on a separate reference zval, pointing to the zval for the current value. This additional zval is not currently shown by debug_zval_dump().

Because debug_zval_dump() takes its input as normal parameters, passed by value, the copy on write technique will be used to pass them: rather than copying the data, the refcount will be increased by one for the lifetime of the function call. If the function modified the parameter after receiving it, then a copy would be made; since it does not, it will show a refcount one higher than in the calling scope.

The parameter passing also prevents debug_zval_dump() showing variables which have been assigned by reference. To illustrate, consider a slightly modified version of the above example:

Example: debug_zval_dump() and value passed by reference

In the example below, although $var1, $var2, and $var3 are linked as references, only the value is passed to debug_zval_dump(). That value is used once by the set of references, and once inside the debug_zval_dump(), therefore shows a refcount of 2.

<?php
$var1 = 'Hello';
$var1 .= ' World';
//pointing three variables as 
//references to the same value
$var2 =& $var1;
$var3 =& $var1;

debug_zval_dump($var1);
debug_zval_dump($var2);
debug_zval_dump($var3);
?>

The output of the above code will be:

string(11) "Hello World" refcount(2)
string(11) "Hello World" refcount(2)
string(11) "Hello World" refcount(2)

More complexity arises because of optimizations made in the engine for different data types. Some types such as integers do not use "copy on write", so do not show a refcount at all. In other cases, the refcount shows extra copies used internally, such as when a literal string or array is stored as part of a code instruction.


❮ PHP Variable Handling Reference