Xdebug Var Dump Settings

How to Prevent xDebug from Truncating var_dump() Output

xdebug is an awesome debugging extension and it has saved me so much time when developing and also when troubleshooting php scripts and WordPress plugins.

When you’re working with xdebug in your PHP development environment and using functions like var_dump() to output some debugging data (logging is better but sometimes you have to do it), you might notice that sometimes not all of your data is shown. This is where the xdebug configuration options come into play, specifically xdebug.var_display_max_depth, xdebug.var_display_max_data, and xdebug.var_display_max_children. These settings help manage how much information xdebug should show you when you dump a variable. Let’s break down what each of these settings means in a developer-friendly, conversational tone. I suspect it’s a security measure and also maybe a protection to not run out of memory when there’s a lot of data.

1. xdebug.var_display_max_depth

This setting controls how deep xdebug goes into your variables when it displays them. Think of it as setting limits on how many levels deep you can peek into your data structures. For example, if you set:

xdebug.var_display_max_depth = 5

xdebug will show you the contents of your variable up to five levels deep. If your variable is an array that contains other arrays or objects nested within each other, xdebug will only expand out to see the contents up to the fifth nested level. Anything deeper than that will not be fully displayed, which helps prevent an overwhelming amount of information from flooding your screen.

If you still don’t see the the full data you can increase the value to 10 or 20 but if you really want to show it all then use -1.

2. xdebug.var_display_max_data

This configuration option determines the maximum amount of data xdebug will show you from your variables. The limit is set in bytes. For instance:

xdebug.var_display_max_data = 512

Here, xdebug will display up to 512 bytes of data when you use var_dump() or similar functions. If the total size of the data in your variable exceeds 512 bytes, xdebug won’t show everything. Instead, it truncates the output to fit within this limit. This is particularly useful for controlling the output size when you’re dealing with large strings or massive arrays.

3. xdebug.var_display_max_children

xdebug.var_display_max_children

Finally, xdebug.var_display_max_children sets the limit on the number of elements (children) xdebug should show for each level within your structures. This setting is also about avoiding information overload. Setting it looks like this:

xdebug.var_display_max_children = 128

With this setting, xdebug will show the first 128 elements of any array or object. If your array has more than 128 elements, the rest will not be displayed in the dump output. This helps keep your debugging output manageable, especially when you accidentally try to dump a large array.

Here’s the full list of xdebug settings and configuration options.

Unlimited

If you really want to show ALL the data without any limitations then use -1 as a value but be careful the server can crash if you have lots of data.

xdebug.var_display_max_depth = -1 
xdebug.var_display_max_children = -1
xdebug.var_display_max_data = -1

The settings above assumed that you’ve edited the php.ini file directly. Keep in mind that there are different settings for CLI (php command line interface) and also when it’s loaded as an Apache module.

CLI: /etc/php/8.3/cli/php.ini
Apache: /etc/php/8.3/apache2/php.ini

Setting the values via .htaccess file

If you can’t modify php.ini or don’t have access to it you can use the following code snippet that you can add to the .htaccess file in your document root folder which is www/htdocs/public_html etc.

You need to be using Apache or openlitespeed in order for these rules to work. You can copy and paste both and Apache will load the appropriate one depending on which php version for loaded.

For php7

# PHP7.x BEGIN Xdebug settings for variable output customization
<IfModule mod_php7.c>
    php_value xdebug.var_display_max_depth 5
    php_value xdebug.var_display_max_data 512
    php_value xdebug.var_display_max_children 128
</IfModule>
# END Xdebug settings

For php 8 (the ifmodule redirect has changed)

# PHP8: BEGIN Xdebug settings for variable output customization
<IfModule php_module.c>
    php_value xdebug.var_display_max_depth 5
    php_value xdebug.var_display_max_data 512
    php_value xdebug.var_display_max_children 128
</IfModule>
# END Xdebug settings

Setting xdebug.var_display data at runtime

if for some reason the suggestions above don’t work for you or you prefer things to be in version control you can use these values in your config file so they are always loaded and first.

<?php
// Set xdebug var dump maximum depth
ini_set('xdebug.var_display_max_depth', 5);

// Set xdebug var dump maximum data size
ini_set('xdebug.var_display_max_data', 512);

// Set xdebug var dump maximum children to display
ini_set('xdebug.var_display_max_children', 128);

Using ini_set() in this way makes your script flexible and adaptable, allowing you to easily tweak how much information xdebug outputs during your development and debugging processes. This approach is ideal for scenarios where you need quick adjustments without global effects.

Leave a Comment

Your email address will not be published. Required fields are marked *