A simple but smart dump(debugging) function using var_dump and func_get_args()

Every php developer knows the drill. Dumping debug info while developing. You either love it or you hate it. There are several solutions. In this post I will discuss those and suggest a lightweight alternative.

  1. Use one of the various php functions:
  2. A library class, like for example Zend_Debug::dump.
  3. Something fancy like xdump or amon.


  • var_dump and var_export both add variable types, however, output of var_dump must be captured with ob_start and var_export is verbose.
  • json_encode is nice, but requires php 5.4 for pretty prints.
  • print_rs output is perfect except for the fact that it doesn’t jot down variable types.
  • All of the php functions require a <pre> tag so the output is formatted.
  • Using Zend_Debug is rather nice, but it requires you to install several of the classes from the library.
  • xdump is an option when javascript accordions are your thing, amon is nice when you want to set it up using remote hosting.

I however like small and scalable functions in my websites to do small stuff like this. Therefore, I wrote the following little snippet to output clean debug code.

function dump() {
    if (!func_num_args() || !defined('DEBUG') || !DEBUG)
    $bt = debug_backtrace();
    $descr = array();

    $fileinfo = '';
    if (isset($bt[0]['file'], $bt[0]['line'])) {

        $fileinfo = basename($bt[0]['file']) . ':' . $bt[0]['line'];

        $file = file($bt[0]['file']);
        $line = $file[$bt[0]['line'] - 1];

        $thisfunc = __FUNCTION__;

        if (preg_match("#$thisfunc\((.+)\)#", $line, $match)) {
            $descr = preg_split('#[, ]+#', $match[1]);

    echo '<div class="debug-dump-wrapper">';
    printf('<h3 class="debug-dump-title">Debug info: %s</h3>', $fileinfo);
    foreach (func_get_args() as $i => $var) {
        $label = sprintf('<span class="debug-label">%s:</span>', (isset($descr[$i])) ? trim($descr[$i]) : 'Parameter ' . ($i + 1));

        $output = ob_get_clean();
        if (!extension_loaded('xdebug')) {

            echo $label;

            echo '<pre class="debug-dump">';

            $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
            $output = htmlspecialchars($output);
            echo $output;

            echo '</pre>';
        } else {
            echo $label, $output;
    echo '</div>';

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd met *