ChatGPT解决这个技术问题 Extra ChatGPT

How to get xdebug var_dump to show full object/array

I am using xdebug (php_xdebug-2.1.2-5.3-vc9.dll) on WAMP. When I use var_dump on a large object or variable it does not show the full variable.

array
'node' => 
  array
    'my_form' => 
      array
        'form' => 
          array
            ...

Without xdebug it shows as should be expected. I looked at the documentation but did not see a solution. Does anyone know how I can fix this so xdebug var_dump shows the full object?

print_r will print the complete array with all nested values.

S
SpazzMarticus

These are configurable variables in php.ini:

; with sane limits
xdebug.var_display_max_depth = 10
xdebug.var_display_max_children = 256
xdebug.var_display_max_data = 1024 


; with no limits
; (maximum nesting is 1023)
xdebug.var_display_max_depth = -1 
xdebug.var_display_max_children = -1
xdebug.var_display_max_data = -1 

Of course, these may also be set at runtime via ini_set(), useful if you don't want to modify php.ini and restart your web server but need to quickly inspect something more deeply.

ini_set('xdebug.var_display_max_depth', 10);
ini_set('xdebug.var_display_max_children', 256);
ini_set('xdebug.var_display_max_data', 1024);

Xdebug settings are explained in the official documentation.


If anyone is unable to get the edits for their php.ini to work correctly or can't find the ini file, the alternative is to change the settings on the fly, which is shown here stackoverflow.com/a/8331138/89211
These values can also be set in the .htaccess file: php_value xdebug.var_display_max_depth 5 php_value xdebug.var_display_max_children 256 php_value xdebug.var_display_max_data 1024
I have visited this answer more than ten times in the last six months. Thank you Michael Berkowski, I am forever in your debt.
@LincolnBergeson I have to look it up all the time myself too.
I've added these settings to both php.ini and .htaccess, restarted the server (on localhost) and still visual studio code won't show all the array values. What am I missing?
C
Chris Schmitz

I know this is a super old post, but I figured this may still be helpful.

If you're comfortable with reading json format you could replace your var_dump with:

return json_encode($myvar);

I've been using this to help troubleshoot a service I've been building that has some deeply nested arrays. This will return every level of your array without truncating anything or requiring you to change your php.ini file.

Also, because the json_encoded data is a string it means you can write it to the error log easily

error_log(json_encode($myvar));

It probably isn't the best choice for every situation, but it's a choice!


I love this answer. You may also have a pretty html output with something like this: return '
'.json_encode($myvar, JSON_PRETTY_PRINT).'
';
Keep in mind that all variables may not be json_encodeable. Objects that don't implement the jsonserializable interface will just return an empty array "{}"
Also worth noting, json_encoding something won't show you the types, so it's more difficult to identify if something matches appropriately.
It is interesting answer. Original!
m
mehov

I know this is late but it might be of some use:

echo "<pre>";
print_r($array);
echo "</pre>";

print_r() is not a replacement for var_dump.
Fantastic answers. Thanks.
@AnrDaemon seriously?
var_dump([false]); print_r([false]);
I know this is late but - print_r() could absolutely be a replacement for var_dump() depending of what you want! I almost never use var_dump() anymore because I often want to show structures of arrays and IMO print_r() shows that much better than var_dump().
r
raveren

Or you can use an alternative:

https://github.com/php-sage/sage

It works with zero set up and has much more features than Xdebug's var_dump anyway. To bypass the nested limit on the fly with Sage, just use

 +d( $variable ); // append `+` to the dump call

B
Boaz

Checkout Xdebbug's var_dump settings, particularly the values of these settings:

xdebug.var_display_max_children
xdebug.var_display_max_data
xdebug.var_display_max_depth

T
The Onin

I'd like to recommend var_export($array) - it doesn't show types, but it generates syntax you can use in your code :)


s
some_guy

Sometimes var_export in a file can be super useful.

file_put_contents(__DIR__.'/temp.txt', var_export($var, true), FILE_APPEND);

For example, if you are debugging something on the production server.