php-doc-en/reference/var/functions/debug-zval-dump.xml
Daniel Egeberg 96c9d88bad Converted to utf-8
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@297028 c90b9560-bf6c-de11-be94-00142212c4b1
2010-03-28 22:10:10 +00:00

185 lines
5.1 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.debug-zval-dump" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>debug_zval_dump</refname>
<refpurpose>Dumps a string representation of an internal zend value to output</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>void</type><methodname>debug_zval_dump</methodname>
<methodparam><type>mixed</type><parameter>variable</parameter></methodparam>
</methodsynopsis>
<para>
Dumps a string representation of an internal zend value to output.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>variable</parameter></term>
<listitem>
<para>
The variable being evaluated.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.void;
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>debug_zval_dump</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
$var1 = 'Hello World';
$var2 = '';
$var2 =& $var1;
debug_zval_dump(&$var1);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
&string(11) "Hello World" refcount(3)
]]>
</screen>
</example>
</para>
<note>
<title>Beware the <literal>refcount</literal></title>
<para>
The <literal>refcount</literal> value returned by this function is
non-obvious in certain circumstances. For example, a developer might
expect the above example to indicate a <literal>refcount</literal> of
<literal>2</literal>. The third reference is created when actually
calling <function>debug_zval_dump</function>.
</para>
<para>
This behavior is further compounded when a variable is not passed to
<function>debug_zval_dump</function> by reference. To illustrate, consider
a slightly modified version of the above example:
</para>
<para>
<example>
<title/>
<programlisting role="php">
<![CDATA[
<?php
$var1 = 'Hello World';
$var2 = '';
$var2 =& $var1;
debug_zval_dump($var1); // not passed by reference, this time
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(11) "Hello World" refcount(1)
]]>
</screen>
</example>
</para>
<para>
Why <literal>refcount(1)</literal>? Because a copy of <literal>$var1</literal> is
being made, when the function is called.
</para>
<para>
This function becomes even <emphasis>more</emphasis> confusing when a
variable with a <literal>refcount</literal> of <literal>1</literal> is
passed (by copy/value):
</para>
<para>
<example>
<title/>
<programlisting role="php">
<![CDATA[
<?php
$var1 = 'Hello World';
debug_zval_dump($var1);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(11) "Hello World" refcount(2)
]]>
</screen>
</example>
</para>
<para>
A <literal>refcount</literal> of <literal>2</literal>, here, is extremely
non-obvious. Especially considering the above examples. So what's
happening?
</para>
<para>
When a variable has a single reference (as did <literal>$var1</literal>
before it was used as an argument to <function>debug_zval_dump</function>),
PHP's engine optimizes the manner in which it is passed to a function.
Internally, PHP treats <literal>$var1</literal> like a reference (in that
the <literal>refcount</literal> is increased for the scope of this
function), with the caveat that <emphasis>if</emphasis> 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."
</para>
<para>
So, if <function>debug_zval_dump</function> 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 <literal>refcount</literal> to
be incremented to <literal>2</literal> for the scope of the function call.
</para>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>var_dump</function></member>
<member><function>debug_backtrace</function></member>
<member><link linkend="language.references">References Explained</link></member>
<member><link xlink:href="&url.derick.references;">References Explained (by Derick Rethans)</link></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->