I'd like to capture the output of var_dump
to a string.
The PHP documentation says;
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).
What would be an example of how that might work?
print_r()
isn't a valid possibility, because it's not going to give me the information that I need.
The var-dump is display the datatype as well as value while echo only display the value. Explanation: In Php the var-dump function is used for displaying the datatype as well the value . The var-dump () does not return anythings it means there is no datatype of var-dump() function.
The var_dump() function is used to dump information about a variable. This function displays structured information such as type and value of the given variable. Arrays and objects are explored recursively with values indented to show structure. This function is also effective with expressions.
var_dump() displays values along with data types as output. print_r() displays only value as output. It does not have any return type. It will return a value that is in string format.
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:
$debug = var_export($my_var, true);
I prefer this one-liner to using ob_start
and ob_get_clean()
. I also find that the output is a little easier to read, since it's just PHP code.
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 (but may not give you quite as much information about the variable, especially if you're working with resources).
$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);
$debug_export
in above example): 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, )
$debug_dump
in above example): 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 }
$debug_printr
in above example):Array ( [bool] => [int] => 1 [float] => 3.14 [string] => hello world [array] => Array ( ) [object] => stdClass Object ( ) [resource] => Resource id #4 [null] => )
var_export
does not handle circular referencesIf you're trying to dump a variable with circular references, calling var_export
will result in a PHP warning:
$circular = array(); $circular['self'] =& $circular; var_export($circular);
Results in:
Warning: var_export does not handle circular references in example.php on line 3 array ( 'self' => array ( 'self' => NULL, ), )
Both var_dump
and print_r
, on the other hand, will output the string *RECURSION*
when encountering circular references.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With