PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<unsetvar_export>
view the version of this page
Last updated: Sat, 29 Oct 2005

var_dump

(PHP 3 >= 3.0.5, PHP 4, PHP 5)

var_dump -- Dumps information about a variable

Description

void var_dump ( mixed expression [, mixed expression [, ...]] )

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.

In PHP 5 only public, private and protected properties of objects will be returned in the output.

Tip: As with anything that outputs its result directly to the browser, you can use the output-control functions to capture the output of this function, and save it in a string (for example).

Parameters

expression

The variable you want to export.

Return Values

No value is returned.

Examples

Example 1. var_dump() example

<?php
$a
= array(1, 2, array("a", "b", "c"));
var_dump($a);
?>

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"
  }
}
<?php

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

?>

The above example will output:

float(3.1)
bool(true)



add a note add a note User Contributed Notes
var_dump
andre at webkr dot de
04-Oct-2005 05:45
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.

Example:
<?php
$a
['foo'] = 'other';
$a['bar'] = 'i_have_ref';
$b =& $a['bar'];

var_dump($a);
var_dump($b);
?>

Result:
array(2) {
 ["foo"]=>
  string(5) "other"
 ["bar"]=>
  &string(10) "i_have_ref"
}
string(10) "i_have_ref"
Dennis Pallett
10-Aug-2005 06:57
<?php
function show($data, $func = "print_r", $return_str = false){
  
ob_start();
  
$func($data);
  
$output = '<pre>'.htmlspecialchars(ob_get_contents()).'</pre>';
  
ob_end_clean();
   if(
$return_str) return $output; else echo $output;
}
?>

From http://keithdevens.com/weblog/archive/2004/Jul/15/PHP.show

Works perfectly for me. Makes it extermely easy to view variables, arrays and objects.
ospinto at hotmail dot com
07-Aug-2005 12:43
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 http://dbug.ospinto.com
As
30-Jun-2005 04:58
The dumped variables don't have html entities encoded. So if your variable contains < in the data, your browser will see it as the start of an html tag. Additionally the dump has lots of => in it, which are also not escaped, although your browser does manage to figure out to display them as is. But they are errrors though.

Wrapping it in PRE doesn't help. Using the XMP tag will work, but it's a depreciated tag. I'm using it anyway, since it does what I need, and this function isn't (usually) used for production code anyway.

<?php
echo "<XMP>";
var_dump(get_defined_vars());
echo
"</XMP>";
?>

XMP is your best bet, or use output control and the htmlspecialchars function.
edwardzyang at thewritingpot dot com
20-Mar-2005 05:06
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.

<?php

function var_dump_pre($mixed = null) {
  echo
'<pre>';
 
var_dump($mixed);
  echo
'</pre>';
  return
null;
}

?>

This one returns the value of var_dump instead of outputting it.

<?php

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).
dwatson at planandgrow dot com
09-Mar-2005 07:02
To make sure to get the nice indenting of nested arrays, you may have to put html-<pre> tags around the var_dump():

Where $a is an array--
echo "<pre>";
var_dump($a);
echo "</pre>";
anon
28-Jan-2005 09:31
var_dump(get_defined_vars());
will dump all defined variables to the browser.

<unsetvar_export>
 Last updated: Sat, 29 Oct 2005
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This mirror generously provided by: Web Hosting Talk
Last updated: Wed Nov 16 09:33:43 2005 EST