How to: enable colors in PHP var_dump()

Not that long ago I was fidling around with my Xdebug settings and discovered by accident that you can enable a nice color formatted var_dump by changing just one small setting. It improves readability a lot and also color highlights errors which obviously comes in very handy when debugging code.


For example, this is the regular PHP var_dump() method output without Xdebug installed.


And this is the one with Xdebug installed & the color highlighting option enabled. As you can see it highlights each type in a different color and also the array and string lengths. It also shows the filename and line number where you're var_dumping from.


And below an example of a highlighted PHP error.


Bonus: if you install/enable this on a webserver it will also do this in the browser.



Install the package

For Mac users, assuming you're using homebrew, brew install josegonzalez/php/php55-xdebug
For Linux users: sudo apt-get install php5-xdebug

Enable the color highlighting

Run php --ini to see your ini files, this should output something like the following.

➜  web git:(develop) php --ini
Configuration File (php.ini) Path: /usr/local/etc/php/5.5
Loaded Configuration File:         /usr/local/etc/php/5.5/php.ini
Scan for additional .ini files in: /usr/local/etc/php/5.5/conf.d
Additional .ini files parsed:      /usr/local/etc/php/5.5/conf.d/ext-blitz.ini,

The last line shows the location of our Xdebug ini file: /usr/local/etc/php/5.5/conf.d/ext-xdebug.ini. Open it up with your editor of choice and add the following line at the end of the file: xdebug.cli_color=1.

Optional: restart php-fpm

If you're running php-fpm and want to enable this on your website, you'll need to restart this so it loads the updated configuration files: sudo service php5-fpm restart.

That's it, enjoy!