What is var_dump?

The var_dump manual page [ http://php.net/var_dump ] states that var_dump 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.

Description :

Php Code
void var_dump ( mixed $expression [, mixed $... ] )
[ad type=”banner”]

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()
[ http://us3.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo ] method (implemented in PHP 5.6.0).

Example #1

var_dump() example:

Php Code
<?php

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

?>

The above example will output:

Php Code
float(3.1)
bool(true)

The problem is that var_dump outputs its result directly to the browser, how can you capture its output in a string variable? read on…

Using output control functions for the solution:

Output control functions can be used to capture and redirect the standard output. For more details, read the PHP manual on output control functions
[ http://php.net/manual/en/ref.outcontrol.php ], of course.

Php Code
<?php

function varDumpToString($var) {
ob_start();
var_dump($var);
$result = ob_get_clean();
return $result;
}

//
//Example usage:
// $data = array('first', 'second', 'third');
// $result = varDumpToString($data);
//
[ad type=”banner”]

Sample code:

Use output buffering:

Php Code
<?php
ob_start();
var_dump($someVar);
$result = ob_get_clean();
?>

A smart alternative to PHP’s “var_dump” function

The “var_debug” function is a smart alternative to PHP’s “var_dump” function, which limits the output and prints the file path and line of the invocation.

File path and line of invocation

The “var_debug” function outputs the file path and line number of the invocation. So even when your “var_dump” calls all say the same you can still see what happened. To accomplish this, it uses the PHP “debug_backtrace” function.

It sets the “DEBUG_BACKTRACE_IGNORE_ARGS” option to reduce memory usage. From the produced backtrace, it gets the first element that holds a file path and line number and it adds that to the start of the output.

Limit the output:

The “var_debug” function limits the output with the following default rules:

Only the first 100 characters of each string are logged (full length is shown)

Only the first 25 elements of an array are logged (full length is shown)

Only the first 10 levels of nested objects/arrays are logged

These three limits can be set using three optional parameters in the above order. Hence, calling “var_debug($variable)” is equal to calling “var_debug($variable,100,25,10)”.

Modifications:

If you would like the function to echo HTML-printable output, you can replace the last line that says “echo $string;” with the following:

Php Code
echo nl2br(str_replace(' ',' ',htmlentities($string)));

Replace “echo $string;” with “return $string;” if you want to use the output for logging purposes in some other function.

var_export

You may want to check out var_export — while it doesn’t provide the same output as var_dump it does provide a second $return parameter which will cause it to return its output rather than print it:

Php Code
$debug = var_export($my_var, true);
[ad type=”banner”]

The difference between var_dump and var_export is that var_export returns a “parsable string representation of a variable” while var_dump simply dumps information about a variable.

What this means in practice is that var_export gives you valid PHP code.

Demo:

Php Code
$demo = array(
"bool" => false,
"int" => 1,
"float" => 3.14,
"string" => "hello world",
"array" => array(),
"object" => new stdClass(),
"resource" => tmpfile(),
"null" => 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);

The difference in output:

var_export ($debug_export in above example)

Php Code
array (
'bool' => false,
'int' => 1,
'float' => 3.1400000000000001,
'string' => 'hello world',
'array' =>
array (
),
'object' =>
stdClass::__set_state(array(
)),
'resource' => NULL, // Note that this resource pointer is now NULL
'null' => NULL,
):

var_dump ($debug_dump in above example)

Php Code
array(8) {
["bool"]=>
bool(false)
["int"]=>
int(1)
["float"]=>
float(3.14)
["string"]=>
string(11) "hello world"
["array"]=>
array(0) {
}
["object"]=>
object(stdClass)#1 (0) {
}
["resource"]=>
resource(4) of type (stream)
["null"]=>
NULL
}

print_r ($debug_printr in above example):

Php Code
Array
(
[bool] =>
[int] => 1
[float] => 3.14
[string] => hello world
[array] => Array
(
)

[object] => stdClass Object
(
)

[resource] => Resource id #4
[null] =>
)

Caveat: var_export does not handle circular references

If you’re trying to dump a variable with circular references, calling var_export will result in a PHP warning:

Php Code
$circular = array();
$circular['self'] =& $circular;
var_export($circular);

Results in:

Php Code
Warning: var_export does not handle circular references in example.php on line 3
array (
'self' =>
array (
'self' => NULL,
),
)
[ad type=”banner”]

Both var_dump and print_r, on the other hand, will output the string *RECURSION* when encountering circular references.

Categorized in: