An alternative to 'var_dump' in PHP
Whilst working with PHP, I seem to use ‘var_dump’ a ridiculous amount, its a debugging must-have. However, it does fall short in a few ways, especially in how it does not take into consideration that the function is almost always displayed in a HTML page - switching to the page’s source can become a pain. Due to the pitfalls a host of projects such as Krumo and Kint have cropped up to cater for these needs. As well as these projects, if you have XDebug installed it will replace the default ‘var_dump’ function with its own implementation that outputs the information with well-needed styling. For me though, I do not need all bells n’ whistles that these provide, my base requirements are:
- Better presentation on a HTML page, no viewing of source necessarily
- Output of the Page, Class, Function, Line that called the function
- Ability to end script execution immediately after outputting the information (for easier debugging)
So as a result of these requirements I created the two simple functions below. I am sure there are many similar implementations available online, but these two are serving me well.