If you wrap the output of functions like print_r
, var_dump
, and var_export
in a <pre>
tag, it will be relatively nicely formatted.
The reason is because the output of the functions is plain text, but when you look at it in a browser, the browser considers it HTML. The new line characters in the plaintext output are not meaningful to HTML, as new lines are ignored.
To see this in action, try viewing the source - you'll see the nicely formatted output there.
The pre
HTML tag tells a browser "everything inside of this block is pre-formated". New lines are treated as new lines, spacing is respected (HTML also doesn't care about sequences of spaces).
So, you're left with something like this:
echo '<pre>'.print_r($my_array).'</pre>';
Instead of doing that all over my code, I like to use a composite function like this (I call it print_p
so it is like typing print_r
)
function print_p($value = false, $exit = false, $return=false, $recurse=false) {
if ($return === true && $exit === true)
$return = false;
$tab = str_repeat(" ", 8);
if ($recurse == false) {
$recurse = 0;
$output = '<div style="width:100%; border: 2px dotted red; background-color: #fbffd6; display: block; padding: 4px;">';
$backtrace = debug_backtrace();
$output .= '<b>Line: </b>'.$backtrace[0]['line'].'<br>';
$output .= '<b>File: </b> '.$backtrace[0]['file'].'<br>';
$indent = "";
} else {
$output = '';
$indent = str_repeat(" ", $recurse * 8);
}
if (is_array($value)) {
if ($recurse == false) {
$output .= '<b>Type: </b> Array<br>';
$output .= "<br>array (<br>";
} else {
$output .= "array (<br>";
}
$items = array();
foreach ($value as $k=>$v) {
if (is_object($v) || is_array($v))
$items[] = $indent.$tab."'".$k."'=>".print_p($v, false, true, ($recurse+1));
else
$items[] = $indent.$tab."'".$k."'=>".($v===null ? "NULL" : "'".$v."'");
}
$output .= implode(',<br>', $items);
if ($recurse == false)
$output .= '<br>)';
else
$output .= '<br>'.$indent.')';
} elseif (is_object($value)) {
if ($recurse == false) {
$output .= '<b>Type: </b> Object<br>';
$output .= '<br>object ('.get_class($value).'){'."<br>";
} else {
$output .= "object (".get_class($value)."){<br>";
}
// needed conditional because base class function dump is protected
$vars = get_object_vars($value);
$vars = (is_array($vars) == true ? $vars : array());
$items = array();
foreach ($vars as $k=>$v) {
if (is_object($v) || is_array($v))
$items[] = $indent.$tab."'".$k."'=>".print_p($v, false, true, ($recurse+1));
else
$items[] = $indent.$tab."'".$k."'=>".($v===null ? "NULL" : "'".$v."'");
}
$output .= implode(',<br>', $items);
$vars = get_class_methods($value);
$items = array();
foreach ($vars as $v) {
$items[] = $indent.$tab.$tab.$v;
}
$output .= '<br>'.$indent.$tab.'<b>Methods</b><br>'.implode(',<br>', $items);
if ($recurse == false)
$output .= '<br>}';
else
$output .= '<br>'.$indent.'}';
} else {
if ($recurse == false) {
$output .= '<b>Type: </b> '.gettype($value).'<br>';
$output .= '<b>Value: </b> '.$value;
} else {
$output .= '('.gettype($value).') '.$value;
}
}
if ($recurse == false)
$output .= '</div>';
if ($return === false)
echo $output;
if ($exit === true)
die();
return $output;
}
... then you do this:
print_p($my_array);
...and get the output:

This is nice because it a) will take any type of variable, objects, arrays, strings, and b) tell you where the output is coming from. It can get really frustrating if you lose track of where you had put a debugging message and have to spend time searching all over for it! :)