If you are a developer and familiar with php programming language, you will (sometimes) use var_dump() to dump an information about a variable. It can be a string, integer, array or even an object. Sometimes variable (especially an array or object) that you want to dump have so many levels deep. Unfortunately, default settings for var_dump() if you have xdebug installed (or you use wamp or xampp as php server), it’s only accept for three levels deep, unless you change the settings.
If you don’t wanna mess up with that settings, probably change php.ini or you have xdebug.ini, or maybe you don’t have access to do it, alternatively you can use my module here, it is called gsVarDump. It’s pure php without needed any external settings. You just have to include it along with your php file(s).
1. Story behind gsVarDump
Last week, I wrote a php project which had array variable that I wanted to dump. The array variable had array inside it and at the inside of array value had also array and so on (multilevel array). I wanted to dump this array and used var_dump(). Unfortunately, var_dump() accept only three levels deep on my php server (I’m using wamp server). I searched the internet about this and found that I had to change the xdebug.ini or php.ini. Since I’m a noob, and didn’t wanted to mess up with that, I decided to create it my self, a pure php class module.
Before I decided to create my own var_dump, I looked up on the net with thought that maybe someone else has already did. I found Kint, maybe its best implemented for this, but what I wanted was exactly the same style as php var_dump. Kint has its own style. So, I decided to create a simpler one, it’s gsVarDump.
2. Using gsVarDump
Using gsVarDump is easy, just download the module and place it along with your php file(s). Inside the php file, you have to include the module as example below.
After you initiate the module, you can directly dump a variable. To dump a variable, there are two ways to do it. The simplest way to do this is just call the vardump() function. See below.
vardump($vardump [,$limit [,$use_htmlcode]]);
Note: like php var_dump, if you call vardump function it will directly show the result. You can’t store the result to a new variable. If you want to store the result, use OOP-style instead.
In this way, you don’t need to create an object, the function will do it for you.
There are four properties on vardump().
- $vardump : the variable you want to dump.
- $limit (optional) : how much levels deep you want to display, default is 0 which mean unlimited.
- $use_htmlcode (optional) : use if you want to display the result with html-design, the default is true. If you set it to false, the return value is just plain variable without html code.
- $skin (optional) : which refer to css theme to beautify the dump structure to make it nicer look.
The other way to dump a variable is use OOP-style. First, create new gsVarDump object. See below.
$dump = new gsVarDump($skin); $string = $dump->vardump($vardump [,$limit [,$use_htmlcode]]); echo $string;
Unlike directly calling gs_vardump() function, this way you can store the result to a variable because the return of $dump->vardump() is a string (html code included). To omit the html code of returned value, set the $use_htmlcode value to false. The properties are the same as gs_vardump() except for the $skin theme, this way you define a skin theme in different method.
3. Set themes on gsVarDump
gsVarDump has theme. If you want the style exactly look like the php var_dump() with xdebug-installed output, you can use default theme. To use it, refer $skin to where default css theme located. See example below.
$dump = new gsVarDump('themes/default.css');
At the example, css default theme is in the folder themes. Of course, you can modify the default css to match it with your own style, or create a new theme based on default css theme. Default css theme is included in the download package.
If you directly set theme when creating the object, it will force the html css link to be placed to where the object is created. If you want a clean html structure, leave $skin variable to empty then call useSkin() function and place it on html head where the css link is ordinary placed. See example below.
$dump = new gsVarDump(); //this is somewhere on html head $dump->useSkin('themes/default.css');
If you don’t wanna messed up with themes, and make this module independent without needed any external css sources, you have to take a look at the gsVarDump code module and change value of a constant IN_LINE_CSS_STYLE to true (default is false). If you set it to true, any themes setting will be ignored and default built in css which is in line with html code span/div will be used.
4. gsVarDump in Action
If you want to try gsVarDump, just download it on Github and place it to the directory of your php files. In the packages, there is example for you to show how gsVarDump is used. This is just a little introduction for you and comment here if you want to ask something.