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
Jquery Mobile: How to Correctly Submit Form Data
PHP Emitting 500 on Errors - Where Is This Documented
Symfony2 and Date_Default_Timezone_Get() - It Is Not Safe to Rely on the System's Timezone Settings
Url Rewriting in PHP Without Htaccess
Back Button Re-Submit Form Data ($_Post)
How to Extract Query Parameters from a Url String in PHP
How to Delete an Object's Property in PHP
Understanding PHP & (Ampersand, Bitwise And) Operator
How to Call a PHP Script/Function on a HTML Button Click
Casting an Array with Numeric Keys as an Object
Fatal Error: Using $This When Not in Object Context
PHP Function to Build Query String from Array
Converting Object to JSON and JSON to Object in PHP, (Library Like Gson for Java)