Source of: /manual/en/function.debug-zval-dump.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.var.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'function.debug-zval-dump.php',
1 => 'debug_zval_dump',
),
'up' =>
array (
0 => 'ref.var.php',
1 => 'Variable handling Functions',
),
'prev' =>
array (
0 => 'ref.var.php',
1 => 'Variable handling Functions',
),
'next' =>
array (
0 => 'function.doubleval.php',
1 => 'doubleval',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="function.debug-zval-dump" class="refentry">
<div class="refnamediv">
<h1 class="refname">debug_zval_dump</h1>
<p class="verinfo">(PHP 4 >= 4.2.0, PHP 5)</p><p class="refpurpose"><span class="refname">debug_zval_dump</span> — <span class="dc-title">Dumps a string representation of an internal zend value to output</span></p>
</div>
<a name="function.debug-zval-dump.description"></a><div class="refsect1 description">
<h3 class="title">Description</h3>
<div class="methodsynopsis dc-description">
<span class="type"><span class="type void">void</span></span> <span class="methodname"><b>debug_zval_dump</b></span>
( <span class="methodparam"><span class="type"><a href="language.pseudo-types.php#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$variable</tt></span>
)</div>
<p class="para rdfs-comment">
Dumps a string representation of an internal zend value to output.
</p>
</div>
<a name="function.debug-zval-dump.parameters"></a><div class="refsect1 parameters">
<h3 class="title">Parameters</h3>
<p class="para">
</p><dl>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">variable</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
The variable being evaluated.
</p>
</dd>
</dl>
<p>
</p>
</div>
<a name="function.debug-zval-dump.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
No value is returned.
</p>
</div>
<a name="function.debug-zval-dump.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 <b>debug_zval_dump()</b> example</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$var1 </span><span style="color: #007700">= </span><span style="color: #DD0000">'Hello World'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$var2 </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$var2 </span><span style="color: #007700">=& </span><span style="color: #0000BB">$var1</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">debug_zval_dump</span><span style="color: #007700">(&</span><span style="color: #0000BB">$var1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
&string(11) "Hello World" refcount(3)
</pre></div>
</div>
</div><p>
</p>
<blockquote><p><b class="note">Note</b>:
<b>Beware the <i>refcount</i></b><br />
The <i>refcount</i> value returned by this function is
non-obvious in certain circumstances. For example, a developer might
expect the above example to indicate a <i>refcount</i> of
<i>2</i>. The third reference is created when actually
calling <b>debug_zval_dump()</b>.
<br />
This behavior is further compounded when a variable is not passed to
<b>debug_zval_dump()</b> by reference. To illustrate, consider
a slightly modified version of the above example:
<br />
</p><div class="example">
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$var1 </span><span style="color: #007700">= </span><span style="color: #DD0000">'Hello World'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$var2 </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$var2 </span><span style="color: #007700">=& </span><span style="color: #0000BB">$var1</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">debug_zval_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$var1</span><span style="color: #007700">); </span><span style="color: #FF8000">// not passed by reference, this time<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
string(11) "Hello World" refcount(1)
</pre></div>
</div>
</div><p>
<br />
Why <i>refcount(1)</i>? Because a copy of <i>$var1</i> is
being made, when the function is called.
<br />
This function becomes even <em class="emphasis">more</em> confusing when a
variable with a <i>refcount</i> of <i>1</i> is
passed (by copy/value):
<br />
</p><div class="example">
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$var1 </span><span style="color: #007700">= </span><span style="color: #DD0000">'Hello World'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">debug_zval_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$var1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
string(11) "Hello World" refcount(2)
</pre></div>
</div>
</div><p>
<br />
A <i>refcount</i> of <i>2</i>, here, is extremely
non-obvious. Especially considering the above examples. So what's
happening?
<br />
When a variable has a single reference (as did <i>$var1</i>
before it was used as an argument to <b>debug_zval_dump()</b>),
PHP's engine optimizes the manner in which it is passed to a function.
Internally, PHP treats <i>$var1</i> like a reference (in that
the <i>refcount</i> is increased for the scope of this
function), with the caveat that <em class="emphasis">if</em> 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."
<br />
So, if <b>debug_zval_dump()</b> 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 <i>refcount</i> to
be incremented to <i>2</i> for the scope of the function call.
<br />
</p></blockquote>
</div>
<a name="function.debug-zval-dump.seealso"></a><div class="refsect1 seealso">
<h3 class="title">See Also</h3>
<p class="para">
</p><ul class="simplelist">
<li class="member"><a href="function.var-dump.php" class="function" rel="rdfs-seeAlso">var_dump()</a> - Dumps information about a variable</li>
<li class="member"><a href="function.debug-backtrace.php" class="function" rel="rdfs-seeAlso">debug_backtrace()</a> - Generates a backtrace</li>
<li class="member"><a href="language.references.php" class="link">References Explained</a></li>
<li class="member"><a href="http://derickrethans.nl/php_references_article.php" class="link external">» References Explained (by Derick Rethans)</a></li>
</ul><p>
</p>
</div>
</div><?php manual_footer(); ?>