Easy PHP debugging with Kint


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 print_r and 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.

Installation

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/’;

Basic Usage

The simplest way to use Kint to output debug information is to use the following. This will allow you to dump the PHP $_SERVER variable.

<?php
require '/kint/Kint.class.php';
Kint::dump($_SERVER);

This will display something like the following (content truncated for easier display).

kint-1

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);

Additional features

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.

<?php
require '/kint/Kint.class.php';
 
$data_array = array("roy mallroy", "james madison",
                    "james@somemail.com",
                    1234567,
                    1234567890,
                    "#C0C0C0");
 
$object = new stdClass();
$object->name = 'Standard Class';
$object->id = 123;
 
Kint::dump(time(), $object, $data_array);

kint-2

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).

Kint::trace();

kint-3

sd() and 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.

Kint::enabled(false);

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

Configuration

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.

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

4 Responses

1

Maciej

November 7th, 2013 at 3:28 pm

You should rather use a debugger for debugging…

2

Douglas

November 8th, 2013 at 4:04 am

Thanks for the article. I had never heard of Kint before but it looks interesting. Will give it a try.

3

Ante Aljinovic

November 8th, 2013 at 4:37 am

4

Stas Ustimenko

February 6th, 2014 at 1:21 am

I prefer to debug my PHP code via Codelobster – http://www.codelobster.com

Your thoughts