views:

1726

answers:

9

I personally use var_dump, but lots of people like print_r.

What does everyone use? Pros and Cons?

Does someone have a special home brew function of their own?

+3  A: 

i use print_r() because i like the pretty array structure... but var_dump does give you a bit more information (like types)

$obj = (object) array(1, 2, 3);

// output of var_dump:
object(stdClass)#1 (3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

// output of print_r
stdClass Object
(
  [0] => 1
  [1] => 2
  [2] => 3
)
Owen
+3  A: 

I use these custom functions depending on whether I'm dealing with an array or a single value:

function show($array)
{
    echo '<pre>';
    print_r($array);
    echo '</pre>';
}

function prn($var)
{
    echo '<br/>' . $var . '<br/>';
}

I find that these functions simplify troubleshooting because I generally end up needing to format the output so that I can go through it easily right on screen.

For more complex troubleshooting, we use an extended version of the Exception class that will email a stack trace along with a specific error message. This gives me the functions that were involved, what files were involved and what line or lines were involved in the error as well as whatever custom message I created so that I know exactly what's going on. For an added layer of troubleshooting, we also log these errors in the database being accessed.

Noah Goodrich
You could make that a single function using if(is_array($val)) {} else {}
Ross
+2  A: 

I just user print_r, along with a couple of wrapper functions to store the various DebugPrint I put in my code and one on the footer to dump the stack in the page (or in a file).

I now try to use XDebug too... :-D

OK, for the record, I give my little functions...

// Primitive debug message storage
// $level = "Info", "Warn", "Error", "Title"
function DebugPrint($toDump, $level = "Info") {
  global $debugMode, $debugDump, $debugCount;

  if ($debugMode != 'N') {
    $debugDump[$debugCount++] = "<div class='Dbg$level'>" . $toDump . "</div>\n";
  }
}

// Initialize debug information collection
$debugMode = 'N'; // N=no, desactivated, P=dump to Web page, F=dump to file
$debugSavePath = 'C:\www\App\log_debug.txt'; // If mode F
$debugDump = array();
$debugCount = 0;

// Primitive debug message dump
function DebugDump() {
  global $debugMode, $debugSavePath, $debugDump, $debugCount;

  if ($debugMode == 'F') {
    $fp = fopen($debugSavePath, "a"); #open for writing
  }
  if ($debugCount > 0) {
    switch ($debugMode) {
    case 'P':
        echo '<div style="color: red; background: #8FC; font-size: 24px;">Debug:<br />
';
        for ($i = 0; $i < $debugCount; $i++) {
            echo $debugDump[$i];
        }
        echo '</div>
';
        break;
    case 'F':
        for ($i = 0; $i < $debugCount; $i++) {
            fputs($fp, $debugDump[$i]);
        }
        break;
//~         default:
//~             echo "debugMode = $debugMode<br />\n";
    }
  }
  if ($fp != null) {
    fputs($fp, "-----\n");
    fclose($fp);
  }
}

// Pre array dump
function DebugArrayPrint($array) {
global $debugMode;

  if ($debugMode != 'N') {
    return "<pre class='ArrayPrint'>" . print_r($array, true) . "</pre>";
  } else return "";    // Gain some microseconds...
}

The interest is to delay the output to the end of the page, avoiding to clutter the real page.

PhiLho
A: 

If you want to avoid sending errors to the browser but want the advantage of var_dump and print_r then take a look at output buffering:

ob_start();
var_dump($this); echo $that; print_r($stuff);
$out = ob_get_contents();
ob_end_clean();

user_error($out);

some good reading in http://www.php.net/manual/en/book.outcontrol.php

Ken
A: 

print_r() usually, but var_dump() provides better information for primitives.

That being said, I do most of my actual debugging with the Zend Server Debugger.

Peter Bailey
+1  A: 

There is something I find very useful that lacks from the standard PHP implementations of variable dump functions, i.e. the ability to just print part of an expression.

Here is a function that solves this:

function dump($val) {
    echo '<pre>'.var_export($val,true).'</pre>';
    return $val;
}

Now I can just put a function call around the expression I need to know the value of without distrupting usual code execution flow like this:

$a=2+dump(2*2);

With use of less-known *var_export* I also eliminated the need to implement output buffering to post-process the result.

Omeoe
+2  A: 

I always use the Xdebug extended var_dump. It gives out a lot of verbose output.

See: http://xdebug.org/docs/display for more details.

Shoan
+1  A: 

When you generate a binary answer (i.e. an image using GD library), then you can use a valid tuned header:

header('X-eleg:'.serialize($yourstuff));

and use the Http-header extension for Firefox to "spy" it.

A: 

var_dump. With XDebug installed it formats and colors the output nicely (no having to put <pre> tags around it).

gaoshan88