(PHP 4 >= 4.2.0, PHP 5)
debug_zval_dump — Dumps a string representation of an internal zend value to output
Dumps a string representation of an internal zend value to output.
variable
The variable being evaluated.
没有返回值。
Example #1 debug_zval_dump() example
<?php
$var1 = 'Hello World';
$var2 = '';
$var2 =& $var1;
debug_zval_dump(&$var1);
?>
以上例程会输出:
&string(11) "Hello World" refcount(3)
Note: Beware the refcount
The refcount value returned by this function is non-obvious in certain circumstances. For example, a developer might expect the above example to indicate a refcount of 2. The third reference is created when actually calling debug_zval_dump().
This behavior is further compounded when a variable is not passed to debug_zval_dump() by reference. To illustrate, consider a slightly modified version of the above example:
<?php
$var1 = 'Hello World';
$var2 = '';
$var2 =& $var1;
debug_zval_dump($var1); // not passed by reference, this time
?>以上例程会输出:
string(11) "Hello World" refcount(1)Why refcount(1)? Because a copy of $var1 is being made, when the function is called.
This function becomes even more confusing when a variable with a refcount of 1 is passed (by copy/value):
<?php
$var1 = 'Hello World';
debug_zval_dump($var1);
?>以上例程会输出:
string(11) "Hello World" refcount(2)A refcount of 2, here, is extremely non-obvious. Especially considering the above examples. So what's happening?
When a variable has a single reference (as did $var1 before it was used as an argument to debug_zval_dump()), PHP's engine optimizes the manner in which it is passed to a function. Internally, PHP treats $var1 like a reference (in that the refcount is increased for the scope of this function), with the caveat that if the passed reference happens to be written to, a copy is made, but only at the moment of writing. This is known as "copy on write."
So, if debug_zval_dump() happened to write to its sole parameter (and it doesn't), then a copy would be made. Until then, the parameter remains a reference, causing the refcount to be incremented to 2 for the scope of the function call.
theriault (2012-06-23 08:43:25)
You can return a consistent refcount value for any variable (and it will work in PHP 5.3+ without problems) using the following function. 0 is returned if the variable passed has no references outside itself or the variable doesn't exist (use isset in conjunction with this function if you need to distinguish between the two).
<?php
/**
* Return the reference count of a variable.
* Returns 0 if a variable has no reference other than itself or doesn't exist.
*/
function refcount(&$var) {
ob_start();
debug_zval_dump(array(&$var));
return preg_replace("/^.+?refcount\((\d+)\).+$/ms", '$1', substr(ob_get_clean(), 24), 1) - 4;
}
# Example
$a = 34;
refcount($a) == 0;
$b = &$a;
refcount($a) == 1;
?>
Using the above function as a dependency, you can determine if two variables reference the same space in memory using another small function:
<?php
/**
* Return whether a and b reference the same memory
*/
function reference(&$a, &$b) {
$d = refcount($b);
$e = &$a;
return refcount($b) != $d;
}
# Example
$a = 3;
$b = 5;
$c = &$a;
$d = &$c;
reference($a, $b) == false;
reference($a, $c) == true;
reference($a, $d) == true;
?>
AmberBlackthorn (2010-10-23 05:35:51)
The add of "Call-time pass-by-reference" as E_DEPRECATED makes it impossible to get the real refcount without getting an error, since <?php error_reporting(E_ALL ^ E_DEPRECATED); ?> and even <?php @debug_zval_dump(&$foo); ?> doesn't change anything.