Skip to content

debug_zval_dump()

Introduction

The debug_zval_dump() function is a built-in function in PHP that provides information about the internal value representation of a PHP variable. Note: This function was deprecated in PHP 7.2 and removed in PHP 8.0. It is only available in legacy PHP versions (up to 7.4). This function is useful for debugging variable references and Zend engine copy-on-write behavior.

Syntax

The syntax of the debug_zval_dump() function is as follows:

The PHP syntax of the debug_zval_dump()

php
void debug_zval_dump(mixed $variable)

The function takes a single parameter, $variable, which is the variable that you want to dump the internal value representation of. The function does not return anything; it only outputs information about the variable to the console.

Example Usage

Here is an example of how to use the debug_zval_dump() function in PHP:

Example of PHP debug_zval_dump()

php
<?php
$a = "hello";
$b = &$a;
debug_zval_dump($a);
debug_zval_dump($b);
?>

In this example, we define two variables $a and $b, where $b is assigned by reference to $a. We then call debug_zval_dump() twice, once for each variable. The output of this code shows the internal value representation, reference count, and is_ref status:

text
string(5) "hello" refcount(3)
string(5) "hello" refcount(3)

Note on refcount: The reference count appears as 3 instead of 2 because debug_zval_dump() internally increments the reference count of the passed variable before printing it.

Conclusion

The debug_zval_dump() function was historically useful for debugging variable references and Zend engine copy-on-write behavior in PHP code. It outputs information about the internal value representation of a PHP variable, including its reference count, data type, and value. By using this function, developers could more easily track down bugs related to variable references and reference counting. For modern PHP 8+ environments, consider using var_dump() for general debugging or xdebug_debug_zval() if Xdebug is installed.

Practice

What does the debug_zval_dump() function in PHP do?

Dual-run preview — compare with live Symfony routes.