Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg

PHP : Function Reference : Variable Handling Functions : var_dump


Dumps information about a variable (PHP 4, PHP 5)
void var_dump ( mixed expression [, mixed expression [, ...]] )

Example 2604. var_dump() example

= array(1, 2, array("a", "b", "c"));

The above example will output:

array(3) {
 array(3) {
   string(1) "a"
   string(1) "b"
   string(1) "c"

= 3.1;
$c = true;
var_dump($b, $c);


The above example will output:


Related Examples ( Source code ) » var_dump

Code Examples / Notes » var_dump


You can also use the PEAR package available at
which parses the variable content in a very pleasant manner, a lot more easier to "follow" than the built-in var_dump() function.
Of course there are many others, but I prefer this one, because it's simply to use.
Just add at the begining of your file:
require('Var_Dump.php'); // make sure the pear package path is set in php.ini
Var_Dump::displayInit(array('display_mode' => 'HTML4_Text'), array('mode' => 'normal','offset' => 4));
then, instead of simply using var_dump($foo), use:
Read the documentation if you're looking for different output layouts.
Vladimir Ghetau


Well, like many others I was looking for a way to get nice dumps of variables. I think I'm about to solve this once and for all; or at least for PHP 5.1. Have a look at my project HLI.
It dumps and highlights not only Arrays and Object but also DOMDocument (as XML), DB results (as table), GD-Images (as PNG), DirectoryIterators (as table), Serialized Strings and many more.
It prints backtrace, object reflection (including PHPDoc) and the position of the dump. Dumps are movable, resizeable and foldable.
It is plugin-based, so its easy to write new dump-modes for other objects/resources. It generates XML output that is parsed by XSLT to HTML and will soon be able to be handled over AJAX (I will do this the next time I program some AJAX stuff).
It runs out of the box (tested with PHP 5.1 and PHP 5.2), but this is only a 0.1 release, so please tell me if something in wrong and I'll be happy to fix it.
Oh, and it's LGPL, so just use and enjoy it. I couldn't work without it any more.


We can all agree that var_dump(); output is very spartan looking. The debug data needs to be organized better, and presented in a graceful way. In the era of Web 2.0 it is somewhat strange to use plain text to dump information. A DHTML powered informatiion dumping tool will be quite better - like the the open-source alternative of var_dump(); -- Krumo (
It renders the output using DHTML and collapsible nodes, it's layout is "skinable" and you can change it to fit your aesthetic taste. Krumo makes the output "human-readable" for real :) Plus it is compliant with both PHP4 and PHP5. Plus it detects "reference recursion". Plus you can use it to dump all various sort of data like debug back-traces, the superglobals ($_SERVER, $_ENV, $_REQUEST, $_COOKIE, $_GET, $_POST, $_SESSION), all the included files, all the declared classes, all the declared constants, all your PHP settings, all your php.ini values (if it is readable), all the loaded extensions, all the HTTP request headers, all the declared interfaces (for PHP5), all the file paths from INCLUDE_PATH, all the values of any particular INI file. Additionally it is designed to be easy to use - for example you can disable all the Krumo dumps instead of cleaning your code out of all print_r()'s and var_dump()'s. Anyway, if you check the site (, you can found a lot of examples, demonstrations, documentation and all sort of helpful information.


Very new to this and really appreciate the contributions of everyone.  I really rely on this site .  BigueNique inspired me with the last post.
I am writing an application that is WAY above my head and am buried in pages of variables and objects.  I really wanted an easier way to read them.
I had two problems - 1. The lists were too long; 2. It was hard to tell where one ended and the other began; 3.  By the time I saw my rendered forms, I forgot where my variable listing was. (Yeah, I can't count, get over it - now you know why this is so hard for me)
I had a variable dumping routine that worked better than the print_r, but elegant_dump was MUCH nicer.
I added a little bit of CSS and javascript to mine, and I have a really quick and easy way to display a colored title with a toggle control to expand or contract the variable structure and contents.
It might be too rudimentary for most of you guys, and I'm not answering anyone's question specifically, but perhaps this creative implementation will make someone's life a little easier.  At a minimum, It might be a good example of mixing in the different languages which was initially a big challenge for me.
simply call: dump_e($var,[$TracerMessage],[$ColorBackground],[$ColorContentBox]);
I have explained my functions as well as posted the required code to make it work here:
It is a little too long to post in this venue.
Best Regards.


will dump all defined variables to the browser.


var_dump prefixes the variable type with & if the variable has more than one reference.
This is only true for variables that are part of an array, not for scalar types.
$a['foo'] = 'other';
$a['bar'] = 'i_have_ref';
$b =& $a['bar'];
array(2) {
 string(5) "other"
 &string(10) "i_have_ref"
string(10) "i_have_ref"


ul_var_dump - dump $var to <ul><li></li></ul>
function ul_var_dump(&$var,$type=0)
echo "<ul type='circle' style='border:1px solid #a0a0a0;padding-bottom:4px;padding-right:4px'>\n<li>";
echo "[array][".count($var)."]";
echo "<ul type='circle' style='border:1px solid #a0a0a0;padding-bottom:4px;padding-right:4px'>\n";
foreach($var as $k=>$v)
echo "<li>\"{$k}\"=>";
echo "</ul>\n";
echo "[".gettype($var)."][{$var}]</li>\n";
echo "</ul>\n";


try to use my vardump function:
all required file you may find here:


test some popular variable dump functions:


Since PHP and Javascript are friends:
function js_dump($js,$n,$z='') {
if($z) {$a = $z . "['" . $n . "']";}
else {$r = 'var '; $a = $n;}
$r .= $a;
if(is_array($js)) {
$r .= ' = new Array();' . "\r\n";
foreach($js as $k => $v) {
if(is_array($v)) {$r .= js_dump($v, $k, $a);}
else {$r .= $a . "['" . $k . "']" . to_js_safe($v);}
else {$r .= to_js_safe($js);}
return $r;
function to_js_safe($js) {return " = '" . addcslashes($js,"\\'") . "';\r\n";}
// example:
$arr = array();
$arr['hello'] = 'ba\'\\"by';
$arr['arr2']['h'] = 'zero';
$arr['arr2']['j'] = 'one';
$arr['arr2']['2']['0'] = 'zero';
$arr['arr2']['2']['1'] = 'one';
$arr[123] = 'but avoid these names in method 1';
// method 1
foreach($arr as $k=>$v) $j .= js_dump($v, $k);
//or method 2 (probably a better idea)
//$j = js_dump($arr,'result');
//$j = 'AJAX=' . rawurlencode($j);
echo $j;
js_dump( variable/array to dump , starter name )
It's not very optimized and I'm no fan of recursion, but for small arrays this works well. I left 2 "\r\n" in there for readability, you should take them out.
NOTE it doesn't check for numerically named variables.
The result is Javascript eval() friendly.
See this tutorial if you are new to AJAX programming:

hayley watson

Or just use your browser's "View Source" option.

zak scott

One of the most used approaches to this I have is:
echo "<pre>";
print_r($array); // or var_dump()
echo "</pre>
This is great for debugging purposes, no need for a long-winded debug class in most cases. Unless maybe you have a gigantic project that you didn't build yourself.


Just created this neat class that dumps a variable in a colored tabular structure similar to the cfdump tag in Coldfusion. Very easy to use and makes it so much easier to see the contents of variable. For examples and download, visit


If you're like me and uses var_dump whenever you're debugging, you might find these two "wrapper" functions helpful.
This one automatically adds the PRE tags around the var_dump output so you get nice formatted arrays.
function var_dump_pre($mixed = null) {
 echo '<pre>';
 echo '</pre>';
 return null;
This one returns the value of var_dump instead of outputting it.
function var_dump_ret($mixed = null) {
 $content = ob_get_contents();
 return $content;
Fairly simple functions, but they're infinitely helpful (I use var_dump_pre() almost exclusively now).


I made a function that I use instead of var_dump. It works well for hierarchical data. The trick is to used nested tables.
May not be the best code but it's not that long so you could improve/modify it. Don't think it will work if any self-referencing is involved.
function variable_to_html($variable) {
if ($variable === true) {
return 'true';
} else if ($variable === false) {
return 'false';
} else if ($variable === null) {
return 'null';
} else if (is_array($variable)) {
$html = "<table border=\"1\">\n";
$html .= "<thead><tr><td><b>KEY</b></td><td><b>VALUE</b></td></tr></thead>\n";
$html .= "<tbody>\n";
foreach ($variable as $key => $value) {
$value = variable_to_html($value);
$html .= "<tr><td>$key</td><td>$value</td></tr>\n";
$html .= "</tbody>\n";
$html .= "</table>";
return $html;
} else {
return strval($variable);
Try it with this completely made-up data:
$meeting = array(
'title' => 'Sales Meeting',
'start_time' => array(
'hours' => 11,
'minutes' => 15,
'ampm' => 'am'
'end_time' => array(
'hours' => 1,
'minutes' => 30,
'ampm' => 'pm'
'attendees' => array(
array('first_name' => 'Bob', 'last_name' => 'Smith', 'email' => ''),
array('first_name' => 'James', 'last_name' => 'Andrews', 'email' => ''),
array('first_name' => 'Tom', 'last_name' => 'Schmoe', 'email' => '')
echo variable_to_html($meeting);


I am working on a pretty large project where I needed to dump a human readable form of whatever into the log files... and I thought var_export was too difficult to read. BigueNique at yahoo dot ca has a nice solution, although I needed to NOT modify whatever was being passed to dump.
So borrowing heavily from BigueNique's (just reworked his function) and someone's idea over in the object cloning page, I came up with the following function.
It makes a complete copy of whatever object you initially pass it, including all recursive definitions and outside objects references, then does the same thing as BigueNique's function. I also heavily reworked what it output, to suit my needs.
function var_log(&$varInput, $var_name='', $reference='', $method = '=', $sub = false) {
static $output ;
static $depth ;
if ( $sub == false ) {
$output = '' ;
$depth = 0 ;
$reference = $var_name ;
$var = serialize( $varInput ) ;
$var = unserialize( $var ) ;
} else {
++$depth ;
$var =& $varInput ;


// constants
$nl = "\n" ;
$block = 'a_big_recursion_protection_block';

$c = $depth ;
$indent = '' ;
while( $c -- > 0 ) {
$indent .= '|  ' ;
// if this has been parsed before
if ( is_array($var) && isset($var[$block])) {

$real =& $var[ $block ] ;
$name =& $var[ 'name' ] ;
$type = gettype( $real ) ;
$output .= $indent.$var_name.' '.$method.'& '.($type=='array'?'Array':get_class($real)).' '.$name.$nl;

// havent parsed this before
} else {
// insert recursion blocker
$var = Array( $block => $var, 'name' => $reference );
$theVar =& $var[ $block ] ;
// print it out
$type = gettype( $theVar ) ;
switch( $type ) {

case 'array' :
$output .= $indent . $var_name . ' '.$method.' Array ('.$nl;
foreach($keys as $name) {
var_log($value, $name, $reference.'["'.$name.'"]', '=', true);
$output .= $indent.')'.$nl;
break ;

case 'object' :
$output .= $indent.$var_name.' = '.get_class($theVar).' {'.$nl;
foreach($theVar as $name=>$value) {
var_log($value, $name, $reference.'->'.$name, '->', true);
$output .= $indent.'}'.$nl;
break ;

case 'string' :
$output .= $indent . $var_name . ' '.$method.' "'.$theVar.'"'.$nl;
break ;

default :
$output .= $indent . $var_name . ' '.$method.' ('.$type.') '.$theVar.$nl;
break ;


// $var=$var[$block];


-- $depth ;

if( $sub == false )
return $output ;

// var_log( $var, '$name' ) ;
Hope it works well for you!


Here's how you use output buffering to save var_dump to a string, as described in the Tip box.
$my_string = ob_get_contents();


Geez! That was a hard one!
Wanted a more 'elegant' way do dump variable contents, but soon discovered the 'PHP reference problem', which makes it hard to deal recursive occurence of the same variable.
The only way I found to detect recursion was to actually modify the variable content (passed by reference) in order to be able to know that it was actually parsed.
References in PHP are quite confusing and often PHP got some strange behaviors....
TIP: don't use FOREACH($array as $key->$value) with arrays containing references...  You'll get the values the references had at the time they were put in the array, not their actual values.  You have to do $value=&$a['key']; (notice the amp) otherwise you're screwed!
// An elegant dump
// By
$elegant_dump_indent = '|&nbsp;&nbsp;&nbsp;&nbsp';
function elegant_dump(&$var, $var_name='', $indent='', $reference='') {
global $elegant_dump_indent;
// first check if the variable has already been parsed
$keyvar = 'the_elegant_dump_recursion_protection_scheme';
$keyname = 'referenced_object_name';
if (is_array($var) && isset($var[$keyvar])) {
// the passed variable is already being parsed!
echo "$indent<b>$var_name</b> (<i>$type</i>) = <font color=\"red\">&amp;$real_name</font>
} else {
   // we will insert an elegant parser-stopper
// do the display
// array?
if (is_array($avar)) {
echo "$indent<b>$var_name</b> (<i>$type($count)</i>) {
foreach($keys as $name) {
elegant_dump($value, "['$name']", $indent.$elegant_dump_indent, $reference);
echo "$indent}
} else
// object?
if (is_object($avar)) {
echo "$indent<b>$var_name</b> (<i>$type</i>) {
foreach($avar as $name=>$value) elegant_dump($value, "-&gt;$name", $indent.$elegant_dump_indent, $reference);
echo "$indent}
} else
// string?
if (is_string($avar)) echo "$indent<b>$var_name</b> (<i>$type</i>) = \"$avar\"
// any other?
else echo "$indent<b>$var_name</b> (<i>$type</i>) = $avar
$b['refers to a']=&$a;
$c['refers to b']=&$b;
$a['refers to c']=&$c;
/* Outputs:
$a (array(1)) {
|    ['refers to c'] (array(1)) {
|    |    ['refers to b'] (array(1)) {
|    |    |    ['refers to a'] (array) = &$a <-- stops recursing there
|    |    }
|    }
Works with objects too: */
/* Outputs:
$d (object) {
|    ->ref2f (object) {
|    |    ->ref2e (object) {
|    |    |    ->ref2d (object) = &$d
|    |    }
|    }


dumping objects that reference each other could lead to infinite recursion
$brother = new Sibling();
$sister = new Sibling();
$brother->sister = $sister;
$sister->brother = $brother;
/* dumps all of $brother's properties, including "sister", which dumps all of $sister's properties, including "brother", etc. */

thriller dot ze

As Bryan said, it is possible to capture var_dump() output to a string. But it's not quite exact if the dumped variable contains HTML code.
You can use this instead:
echo '<pre>'; // This is for correct handling of newlines
echo htmlspecialchars($a,ENT_QUOTES); // Escape every HTML special chars (especially > and < )
echo '</pre>';

Change Language

Follow Navioo On Twitter
eXTReMe Tracker