function.debug-zval-dump.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 182 行
HTML
182 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Dumps a string representation of an internal zend value to output</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ref.var.html">Variable handling Functions</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.doubleval.html">doubleval</a></div> <div class="up"><a href="ref.var.html">Variable handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><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> <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><b>debug_zval_dump</b></b></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#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> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">variable</tt></i></span> <dd> <p class="para"> The variable being evaluated. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> No value is returned. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>debug_zval_dump()</b> example</b></p> <div class="example-contents"><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"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>&string(11) "Hello World" refcount(3)</pre></div> </pre></div> </div> </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 /> <div class="example"> <div class="example-contents"><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"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>string(11) "Hello World" refcount(1)</pre></div> </pre></div> </div> <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 /> <div class="example"> <div class="example-contents"><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"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>string(11) "Hello World" refcount(2)</pre></div> </pre></div> </div> <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> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.var-dump.html" class="function" rel="rdfs-seeAlso">var_dump()</a></li> <li class="member"><a href="function.debug-backtrace.html" class="function" rel="rdfs-seeAlso">debug_backtrace()</a></li> <li class="member"><a href="language.references.html" 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> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ref.var.html">Variable handling Functions</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.doubleval.html">doubleval</a></div> <div class="up"><a href="ref.var.html">Variable handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?