var_dump
This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
All public, private and protected properties of objects will be returned in the output unless the object implements a __debugInfo() method.
As with anything that outputs its result directly to the browser, the output-control functions can be used to capture the output of this function, and save it in a string (for example).
Parameters
Further expressions to dump.
Return Values
Examples
Example #1 var_dump() example
The above example will output:
array(3) < [0]=>int(1) [1]=> int(2) [2]=> array(3) < [0]=>string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" > >
$b = 3.1 ;
$c = true ;
var_dump ( $b , $c );
The above example will output:
See Also
- print_r() — Prints human-readable information about a variable
- debug_zval_dump() — Dumps a string representation of an internal zval structure to output
- var_export() — Outputs or returns a parsable string representation of a variable
- __debugInfo()
User Contributed Notes 16 notes
Keep in mind if you have xdebug installed it will limit the var_dump() output of array elements and object properties to 3 levels deep.
To change the default, edit your xdebug.ini file and add the folllowing line:
xdebug.var_display_max_depth=n
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 ‘
' ;
var_dump ( $mixed );
echo '
‘ ;
return null ;
>
?>
This one returns the value of var_dump instead of outputting it.
function var_dump_ret ( $mixed = null ) ob_start ();
var_dump ( $mixed );
$content = ob_get_contents ();
ob_end_clean ();
return $content ;
>
?>
Fairly simple functions, but they’re infinitely helpful (I use var_dump_pre() almost exclusively now).
I post a new var_dump function with colors and collapse features. It can also adapt to terminal output if you execute it from there. No need to wrap it in a pre tag to get it to work in browsers.
function dump_debug ( $input , $collapse = false ) $recursive = function( $data , $level = 0 ) use (& $recursive , $collapse ) global $argv ;
echo ‘‘ . «\n» ;
>
$type = ! is_string ( $data ) && is_callable ( $data ) ? «Callable» : ucfirst ( gettype ( $data ));
$type_data = null ;
$type_color = null ;
$type_length = null ;
switch ( $type ) case «String» :
$type_color = «green» ;
$type_length = strlen ( $data );
$type_data = «\»» . htmlentities ( $data ) . «\»» ; break;
case «Double» :
case «Float» :
$type = «Float» ;
$type_color = «#0099c5» ;
$type_length = strlen ( $data );
$type_data = htmlentities ( $data ); break;
case «Integer» :
$type_color = «red» ;
$type_length = strlen ( $data );
$type_data = htmlentities ( $data ); break;
case «Boolean» :
$type_color = «#92008d» ;
$type_length = strlen ( $data );
$type_data = $data ? «TRUE» : «FALSE» ; break;
case «NULL» :
$type_length = 0 ; break;
case «Array» :
$type_length = count ( $data );
>
if ( in_array ( $type , array( «Object» , «Array» ))) $notEmpty = false ;
foreach( $data as $key => $value ) if (! $notEmpty ) $notEmpty = true ;
if ( $isTerminal ) echo $type . ( $type_length !== null ? «(» . $type_length . «)» : «» ). «\n» ;
> else $id = substr ( md5 ( rand (). «:» . $key . «:» . $level ), 0 , 8 );
call_user_func ( $recursive , $value , $level + 1 );
>
> else echo $isTerminal ?
$type . ( $type_length !== null ? «(» . $type_length . «)» : «» ) . » » :
«» . $type . ( $type_length !== null ? «(» . $type_length . «)» : «» ) . « » ;
>
> else echo $isTerminal ?
$type . ( $type_length !== null ? «(» . $type_length . «)» : «» ) . » » :
«» . $type . ( $type_length !== null ? «(» . $type_length . «)» : «» ) . « » ;
call_user_func ( $recursive , $input );
>
?>
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.
echo ‘
' ; // This is for correct handling of newlines
ob_start ();
var_dump ( $var );
$a = ob_get_contents ();
ob_end_clean ();
echo htmlspecialchars ( $a , ENT_QUOTES ); // Escape every HTML special chars (especially > and < )
echo '
‘ ;
?>
/**
* Better GI than print_r or var_dump — but, unlike var_dump, you can only dump one variable.
* Added htmlentities on the var content before echo, so you see what is really there, and not the mark-up.
*
* Also, now the output is encased within a div block that sets the background color, font style, and left-justifies it
* so it is not at the mercy of ambient styles.
*
* Inspired from: PHP.net Contributions
* Stolen from: [highstrike at gmail dot com]
* Modified by: stlawson *AT* JoyfulEarthTech *DOT* com
*
* @param mixed $var — variable to dump
* @param string $var_name — name of variable (optional) — displayed in printout making it easier to sort out what variable is what in a complex output
* @param string $indent — used by internal recursive call (no known external value)
* @param unknown_type $reference — used by internal recursive call (no known external value)
*/
function do_dump (& $var , $var_name = NULL , $indent = NULL , $reference = NULL )
$do_dump_indent = «| » ;
$reference = $reference . $var_name ;
$keyvar = ‘the_do_dump_recursion_protection_scheme’ ; $keyname = ‘referenced_object_name’ ;
?php
$type = ucfirst ( gettype ( $avar ));
if( $type == «String» ) $type_color = «» ;
elseif( $type == «Integer» ) $type_color = «» ;
elseif( $type == «Double» )< $type_color = "" ; $type = "Float" ; >
elseif( $type == «Boolean» ) $type_color = «» ;
elseif( $type == «NULL» ) $type_color = «» ;
if( is_array ( $avar ))
$count = count ( $avar );
echo » $indent » . ( $var_name ? » $var_name => » : «» ) . » $type ( $count )
$indent (
» ;
$keys = array_keys ( $avar );
foreach( $keys as $name )
$value = & $avar [ $name ];
do_dump ( $value , «[‘ $name ‘]» , $indent . $do_dump_indent , $reference );
>
echo » $indent )
» ;
>
elseif( is_object ( $avar ))
echo » $indent$var_name $type
$indent (
» ;
foreach( $avar as $name => $value ) do_dump ( $value , » $name » , $indent . $do_dump_indent , $reference );
echo » $indent )
» ;
>
elseif( is_int ( $avar )) echo » $indent$var_name = $type (» . strlen ( $avar ). «) $type_color » . htmlentities ( $avar ). «
» ;
elseif( is_string ( $avar )) echo » $indent$var_name = $type (» . strlen ( $avar ). «) $type_color \»» . htmlentities ( $avar ). «\»
» ;
elseif( is_float ( $avar )) echo » $indent$var_name = $type (» . strlen ( $avar ). «) $type_color » . htmlentities ( $avar ). «
» ;
elseif( is_bool ( $avar )) echo » $indent$var_name = $type (» . strlen ( $avar ). «) $type_color » .( $avar == 1 ? «TRUE» : «FALSE» ). «
» ;
elseif( is_null ( $avar )) echo » $indent$var_name = $type (» . strlen ( $avar ). «) < $type_color >NULL
» ;
else echo » $indent$var_name = $type (» . strlen ( $avar ). «) » . htmlentities ( $avar ). «
» ;