views:

581

answers:

5

Every decent programmer has a print_r or var_dump wrapper they use, love and assign shortcut keys to, why don't we share our favourite ones.

I am using a simple one, here:

function dump($var = null) {
    static $dumpId = 0;

    if($dumpId == 0) {
        echo '<style>pre {margin: 0px; text-align: left; color: #000; padding: 0px; font: 11px/17px Tahoma, Arial, "Lucida Grande", sans-serif; line-height: 17px; border: 3px solid #999; padding: 10px; margin: 10px; background: #eee;}</style>';
    }

    $argList = func_get_args();
    $argNum = func_num_args();

    for($i = 0; $i < $argNum; $i++) {
        // if variable is array or object
        echo '<pre>(Arg [' . $dumpId++ . ']) - ';
        if(is_array($argList[$i]) or is_object($argList[$i])) {
            print_r($argList[$i]);
        } else {
            // if other type of variable
            var_dump($argList[$i]);
        }
        echo '</pre><br />';
    }
}

It basically does nothing more than append some style to the output.

There are also some good ones on the PHP var_dump documentation page..

+10  A: 

You are looking for Krumo.

To put it simply, Krumo is a replacement for print_r() and var_dump(). By definition Krumo is a debugging tool (initially for PHP4/PHP5, now for PHP5 only), which displays structured information about any PHP variable.

Pekka
This is brilliant! Thanks. +1
the_drow
Mawg
+7  A: 
Pascal MARTIN
+6  A: 

I love Firephp plus firebug

  • It writes the logging information out using headers, so it doesn't break AJAX.
  • Gives you a very nice graphical representation of the variables/objects you log
  • Can display file name and line number where each log statement occurs
  • Easy to use in your project, either with a procedural or object-oriented API
Johrn
+1  A: 

Here is mine:

public function varToHtml($var='', $key='') {
     $type = gettype($var);
      $result = '';

      if (in_array($type, array('object','array'))) {
        $result .= '
          <table class="debug-table">
            <tr>
              <td class="debug-key-cell"><b>'.$key.'</b><br/>Type: '.$type.'<br/>Length: '.count($var).'</td>
              <td class="debug-value-cell">';

        foreach ($var as $akey => $val) {
          $result .= sbwDebug::varToHtml($val, $akey);
        }
        $result .= '</td></tr></table>';
      } else {
        $result .= '<div class="debug-item"><span class="debug-label">'.$key.' ('.$type.'): </span><span class="debug-value">'.$var.'</span></div>';
      }

      return $result;
    }

Styled with:

table.debug-table {
  padding: 0;
  margin: 0;
  font-family: arial,tahoma,helvetica,sans-serif;
  font-size: 11px;
}

td.debug-key-cell {
  vertical-align: top;
  padding: 3px;
  border: 1px solid #AAAAAA;
}

td.debug-value-cell {
  vertical-align: top;
  padding: 3px;
  border: 1px solid #AAAAAA;
}

div.debug-item {
  border-bottom: 1px dotted #AAAAAA;
}

span.debug-label {
  font-weight: bold;
}
Damon
+2  A: 

I've been using dBug, which emulates Coldfusion's awesome cfdump tag:

http://dbug.ospinto.com/examples.php

jonscottclark