Skip to content
Advertisement

Formatting print_r or var_dump results

I would like new lines and indents shown in this output

$a = array(1,2,3,4);
print_r($a);

So instead of this:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

I would get something like this:

Array ( 
  [0] => 1 
  [1] => 2 
  [2] => 3 
  [3] => 4
  )

Advertisement

Answer

I suggest using HTML’s <pre> tag (preformatting):

echo "<pre>";print_r($a);echo"</pre>";

EDIT:

To concatenate the output, you must set the second parameter of print_r to true to return the value rather than echoing it:

echo "<pre>" . print_r($a,true) . "</pre>";

print_r ( mixed $value , bool $return = false ) : string|bool
If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to true, print_r() will return the information rather than print it.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement