Make Var_Dump Look Pretty

Make var_dump look pretty

I really love var_export(). If you like copy/paste-able code, try:

echo '<pre>' . var_export($data, true) . '</pre>';

Or even something like this for color syntax highlighting:

highlight_string("<?php\n\$data =\n" . var_export($data, true) . ";\n?>");

Reusable function:

function highlight_array($array, $name = 'var') {
highlight_string("<?php\n\$$name =\n" . var_export($array, true) . ";\n?>");
}

You can do the same with print_r(). For var_dump() you would just need to add the <pre> tags:

echo '<pre>';
var_dump($data);
echo '</pre>';

Colored var_dump() and errors

You get the colored output when you ínstall and enable Xdebug:

Xdebug replaces PHP's var_dump() function for displaying variables. Xdebug's version includes different colors for different types and places limits on the amount of array elements/object properties, maximum depth and string lengths. There are a few other functions dealing with variable display as well.

You can enable/disable this with the ini setting xdebug.overload_var_dump

By default Xdebug overloads var_dump() with its own improved version for displaying variables when the html_errors php.ini setting is set to 1. In case you do not want that, you can set this setting to 0, but check first if it's not smarter to turn off html_errors.

Check the documentation for further information.

Note that you do not want to have the Xdebug extension installed on a production server as it will significantly slow down code execution.

Is there a pretty print for PHP?

Both print_r() and var_dump() will output visual representations of objects within PHP.

$arr = array('one' => 1);
print_r($arr);
var_dump($arr);

Make var_dump look pretty

I really love var_export(). If you like copy/paste-able code, try:

echo '<pre>' . var_export($data, true) . '</pre>';

Or even something like this for color syntax highlighting:

highlight_string("<?php\n\$data =\n" . var_export($data, true) . ";\n?>");

Reusable function:

function highlight_array($array, $name = 'var') {
highlight_string("<?php\n\$$name =\n" . var_export($array, true) . ";\n?>");
}

You can do the same with print_r(). For var_dump() you would just need to add the <pre> tags:

echo '<pre>';
var_dump($data);
echo '</pre>';

var_dump has unreadable output

Seems like your xdebug has been enabled. See below post on how to disable:

https://stackoverflow.com/a/8754934

Any way to make PHP var_dump display deeper nested arrays?

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

where n is your max level.

More information here:
http://www.xdebug.org/docs/display



Related Topics



Leave a reply



Submit