347

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?

dayuloli
  • 16,205
  • 16
  • 71
  • 126
dm03514
  • 54,664
  • 18
  • 108
  • 145

7 Answers7

687

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.

SpazzMarticus
  • 1,218
  • 1
  • 20
  • 40
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • 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 http://stackoverflow.com/a/8331138/89211 – Relequestual Aug 21 '13 at 14:28
  • 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` – Werner Feb 10 '16 at 14:28
  • 3
    I have visited this answer more than ten times in the last six months. Thank you Michael Berkowski, I am forever in your debt. – Lincoln Bergeson Aug 31 '17 at 17:53
  • 3
    @LincolnBergeson I have to look it up all the time myself too. – Michael Berkowski Aug 31 '17 at 17:59
  • The Xdebug documentation has a helpful tool to see the various effects of different values for these settings: https://xdebug.org/docs/display – Erebus Oct 16 '19 at 17:05
  • 1
    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? – Adivate Jan 03 '20 at 21:18
  • @rjrunner44 In VSCode, in order to examine more than the first 32 array elements, I needed to edit my Xdebug launch configuration to add `"xdebugSettings": {"max_children": 256}`, as well as the php.ini settings from this answer. – marlboroman Mar 25 '21 at 03:36
44

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!

Chris Schmitz
  • 20,160
  • 30
  • 81
  • 137
  • 12
    I love this answer. You may also have a pretty html output with something like this: return '
    '.json_encode($myvar, JSON_PRETTY_PRINT).'
    ';
    – David Oct 08 '14 at 10:58
  • 8
    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 "{}" – Kristian Sandström Jul 01 '15 at 08:58
  • 2
    Also worth noting, json_encoding something won't show you the types, so it's more difficult to identify if something matches appropriately. – SEoF Nov 07 '16 at 15:01
  • 1
    It is interesting answer. Original! – Sergio Codev Nov 11 '20 at 11:54
16

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

echo "<pre>";
print_r($array);
echo "</pre>";
ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57
0x58
  • 427
  • 5
  • 13
  • 29
    `print_r()` is **not** a replacement for var_dump. – AnrDaemon Jul 18 '16 at 16:12
  • Fantastic answers. Thanks. – Handsome Nerd Dec 27 '18 at 00:13
  • @AnrDaemon seriously? – Handsome Nerd Dec 27 '18 at 00:14
  • 1
    var_dump([false]); print_r([false]); – AnrDaemon Dec 27 '18 at 10:44
  • 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(). – bestprogrammerintheworld May 22 '20 at 09:37
  • uhm ... print_r just dumps a bunch of text. var_dump gives variable types, array fields and the like. var_dump is obviously for debugging your script. i don't really get @bestprogrammerintheworld – clockw0rk Apr 05 '23 at 09:49
  • @clockw0rk - there are benefits and not using print_r vs var_dump. I typed it could be a replacement DEPENDING on what OP wanted :-) It's not the same thing. I think it's much easier/clearer with print_r. You see if it's a object, array etc, but datatypes as string, int etc is for me almost never important for me to view. Hope that clarified what I meant! :-) – bestprogrammerintheworld Apr 06 '23 at 15:47
15

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
raveren
  • 17,799
  • 12
  • 70
  • 83
  • yeah nice and all, but an external library for just dumping debug data is a bit overkill don't you agree – clockw0rk Apr 05 '23 at 09:50
  • Isn't that like 66% of the work you regularly do - debug data..? Also, are you worried about the diskspace the external library is taking up on your development machine - or what are you worried about when you say "overkill" :) – raveren Apr 05 '23 at 13:02
6

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
Boaz
  • 19,892
  • 8
  • 62
  • 70
Captain Insaneo
  • 470
  • 2
  • 7
5

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

The Onin
  • 5,068
  • 2
  • 38
  • 55
1

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.

some_guy
  • 390
  • 2
  • 14