var_dump

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

var_dump -- 打印变量的相关信息

描述

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

此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值。数组将递归展开值,通过缩进显示其结构。

提示: 为了防止程序直接将结果输出到浏览器,可以使用输出控制函数来捕获此函数的输出,并把它们保存到一个例如 string 类型的变量中。

可以比较一下 var_dump()print_r()

例子 1. var_dump() 示例

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

/* 输出:
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);

/* 输出:
float(3.1)
bool(true)

*/
?>
</pre>


add a note add a note User Contributed Notes
andre at webkr dot de
05-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"
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
edwardzyang at thewritingpot dot com
21-Mar-2005 06: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).
anon
28-Jan-2005 10:31
var_dump(get_defined_vars());
will dump all defined variables to the browser.