<?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 zval structure to output</refpurpose>
 </refnamediv>
 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type>void</type><methodname>debug_zval_dump</methodname>
   <methodparam><type>mixed</type><parameter>value</parameter></methodparam>
   <methodparam rep="repeat"><type>mixed</type><parameter>values</parameter></methodparam>
  </methodsynopsis>
  <para>
   Dumps a string representation of an internal zval (Zend value) structure to output.
   This is mostly useful for understanding or debugging implementation details of the
   Zend Engine or PHP extensions.
  </para>
 </refsect1>
 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term><parameter>value</parameter></term>
     <listitem>
      <para>
       The variable or value to dump.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>values</parameter></term>
     <listitem>
      <para>
       Further variables or values to dump.
      </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';
$var1 .= ' World';
$var2 = $var1;

debug_zval_dump($var1);
?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
string(11) "Hello World" refcount(3)
]]>
    </screen>
   </example>
  </para>
  <note>
   <title>Understanding the <literal>refcount</literal></title>
   <para>
    The <literal>refcount</literal> value shown by this function may be
    surprising without a detailed understanding of the engine's implementation.
   </para>
   <para>
    The Zend Engine uses reference counting for two different purposes:
   </para>
   <para>
    <simplelist>
     <member>
      Optimizing memory usage using a technique called "copy on write",
      where multiple variables holding the same value point to the same copy
      in memory. When any of the variables is modified, it is pointed to a new
      copy in memory, and the reference count on the original is decreased by 1.
     </member>
     <member>
      Tracking variables which have been assigned or passed by reference (see
      <link linkend="language.references">References Explained</link>). This
      refcount is stored on a separate reference zval, pointing to the zval
      for the current value. This additional zval is not currently shown by
      <function>debug_zval_dump</function>.
     </member>
    </simplelist>
   </para>
   <para>
    Because <function>debug_zval_dump</function> takes its input as normal
    parameters, passed by value, the copy on write technique will be used
    to pass them: rather than copying the data, the refcount will be increased
    by one for the lifetime of the function call. If the function modified the
    parameter after receiving it, then a copy would be made; since it does not,
    it will show a refcount one higher than in the calling scope.
   </para>
   <para>
    The parameter passing also prevents <function>debug_zval_dump</function>
    showing variables which have been assigned by reference. To illustrate,
    consider a slightly modified version of the above example:
   </para>
   <para>
    <example>
     <title/>
     <programlisting role="php">
<![CDATA[
<?php
$var1 = 'Hello';
$var1 .= ' World';
// Point three variables as references to the same value
$var2 =& $var1;
$var3 =& $var1;

debug_zval_dump($var1);
?>
]]>
     </programlisting>
     &example.outputs;
     <screen>
<![CDATA[
string(11) "Hello World" refcount(2)
]]>
     </screen>
    </example>
   </para>
   <para>
    Although <varname>$var1</varname>, <varname>$var2</varname>, and
    <varname>$var3</varname> are linked as references, only the
    <emphasis>value</emphasis> is passed to <function>debug_zval_dump</function>.
    That value is used once by the set of references, and once inside the
    <function>debug_zval_dump</function>, so shows a refcount of 2.
   </para>
   <para>
    Further complications arise because of optimisations made in the engine for
    different data types. Some types such as integers do not use "copy on write",
    so do not show a refcount at all. In other cases, the refcount shows extra
    copies used internally, such as when a literal string or array is stored as
    part of a code instruction.
   </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
-->
