My standard debugging tool when using PHP is xdebug. Pairing xdebug with Komodo gives me a very productive environment for building PHP applications. However, testing an application code on a hosted server can be many times frustrating as one has only the
var_dump functions at our disposal. What I need is a library that I can easily install on the server and start using.
Although there are a few debugging libraries around, one of my favorites is Kint. Kint for PHP is a powerful and modern, zero-setup (very important for me) replacement for var_dump(), print_r() and debug_backtrace(). Kint is a tool designed to present your debugging data in the best way possible.
For me installation time is very important. I prefer zero-setup libraries over ones that need endless configuration and dependencies. This is especially important for small projects. Installing Kint is accomplished by just uploading the package to any one of your project directories. In this post we will assume you have installed Kint in the root directory of your site. e.g ‘/httpdocs/kint/’;
The simplest way to use Kint to output debug information is to use the following. This will allow you to dump the PHP
require '/kint/Kint.class.php'; Kint::dump($_SERVER);
This will display something like the following (content truncated for easier display).
You can also add multiple variables to the dump.
Kint::dump($_GET, $_POST, $some_variable);
or use a shorhand as below.
d($_GET, $_POST, $some_variable);
Kint intelligently detects what kind of data is being dumped and provides tailored alternatives to display nicely formatted data. Below is just a small subset of the recognized data set.
require '/kint/Kint.class.php'; $data_array = array("roy mallroy", "james madison", "firstname.lastname@example.org", 1234567, 1234567890, "#C0C0C0"); $object = new stdClass(); $object->name = 'Standard Class'; $object->id = 123; Kint::dump(time(), $object, $data_array);
Another important feature is debug trace. You can add debug trace to your PHP apps using the
trace function. For example a trace added to the WordPress default 'Hello Dolly' plugin is shown below (content truncated to fit).
dd() are shorthands for
s(); die; and
d(); die; respectively. These shorthands can be used to stop execution after a dump.
If you have multiple dump statements throughout the code, you can disable them easily by using the following.
To output variable info in a lightweight text-only (html-free) format, use the following. The output is whitespace-formatted and uses no HTML/CSS/JS.
s($variable); // and sd($variable); // to exit immediately afterwards
You can optionally copy the included config.default.php and rename to config.php to override default values, but that is entirely optional and you're fine with the default options.
One option you may want to modify is the theme settings. The original is given in the option file as below. You can change that to : original, solarized or solarized-dark.
/** @var string name of theme for rich view */ $_kintSettings['theme'] = 'original';
Each option is heavily documented in the configuration file.