Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Try <a href="http://php.net/var_export" rel="noreferrer"><code>var_export</code></a></h1> <p>You may want to check out <a href="http://php.net/var_export" rel="noreferrer"><code>var_export</code></a> — while it doesn't provide the same output as <code>var_dump</code> it does provide a second <code>$return</code> parameter which will cause it to return its output rather than print it:</p> <pre class="lang-php prettyprint-override"><code>$debug = var_export($my_var, true); </code></pre> <h2>Why?</h2> <p>I prefer this one-liner to using <code>ob_start</code> and <code>ob_get_clean()</code>. I also find that the output is a little easier to read, since it's just PHP code.</p> <p>The difference between <code>var_dump</code> and <code>var_export</code> is that <code>var_export</code> returns a <em>"parsable string representation of a variable"</em> while <code>var_dump</code> simply dumps information about a variable. What this means in practice is that <code>var_export</code> gives you valid PHP code (but may not give you quite as much information about the variable, especially if you're working with <a href="http://php.net/manual/en/language.types.resource.php" rel="noreferrer">resources</a>).</p> <h3>Demo:</h3> <pre class="lang-php prettyprint-override"><code>$demo = array( "bool" =&gt; false, "int" =&gt; 1, "float" =&gt; 3.14, "string" =&gt; "hello world", "array" =&gt; array(), "object" =&gt; new stdClass(), "resource" =&gt; tmpfile(), "null" =&gt; null, ); // var_export -- nice, one-liner $debug_export = var_export($demo, true); // var_dump ob_start(); var_dump($demo); $debug_dump = ob_get_clean(); // print_r -- included for completeness, though not recommended $debug_printr = print_r($demo, true); </code></pre> <h2>The difference in output:</h2> <h3>var_export (<code>$debug_export</code> in above example):</h3> <pre class="lang-php prettyprint-override"><code> array ( 'bool' =&gt; false, 'int' =&gt; 1, 'float' =&gt; 3.1400000000000001, 'string' =&gt; 'hello world', 'array' =&gt; array ( ), 'object' =&gt; stdClass::__set_state(array( )), 'resource' =&gt; NULL, // Note that this resource pointer is now NULL 'null' =&gt; NULL, ) </code></pre> <h3>var_dump (<code>$debug_dump</code> in above example):</h3> <pre><code> array(8) { ["bool"]=&gt; bool(false) ["int"]=&gt; int(1) ["float"]=&gt; float(3.14) ["string"]=&gt; string(11) "hello world" ["array"]=&gt; array(0) { } ["object"]=&gt; object(stdClass)#1 (0) { } ["resource"]=&gt; resource(4) of type (stream) ["null"]=&gt; NULL } </code></pre> <h3>print_r (<code>$debug_printr</code> in above example):</h3> <pre><code>Array ( [bool] =&gt; [int] =&gt; 1 [float] =&gt; 3.14 [string] =&gt; hello world [array] =&gt; Array ( ) [object] =&gt; stdClass Object ( ) [resource] =&gt; Resource id #4 [null] =&gt; ) </code></pre> <h2>Caveat: <code>var_export</code> does not handle circular references</h2> <p>If you're trying to dump a variable with circular references, calling <code>var_export</code> will result in a PHP warning:</p> <pre class="lang-php prettyprint-override"><code> $circular = array(); $circular['self'] =&amp; $circular; var_export($circular); </code></pre> <p>Results in: </p> <pre><code> Warning: var_export does not handle circular references in example.php on line 3 array ( 'self' =&gt; array ( 'self' =&gt; NULL, ), ) </code></pre> <p>Both <code>var_dump</code> and <code>print_r</code>, on the other hand, will output the string <code>*RECURSION*</code> when encountering circular references.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload