How do I display print_r on different lines?

If this is what your browser displays:

Array ( [locus] => MK611812 [version] => MK611812.1 [id] => 1588040742 )

And this is what you want:

Array
(
    [locus] => MK611812
    [version] => MK611812.1
    [id] => 1588040742
)

the easy solution is to add the the <pre> format to your code that prints the array:

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

Old question, but I generally include the following function with all of my PHP:

The problem occurs because line breaks are not normally shown in HTML output. The trick is to wrap the output inside a pre element:

function printr($data) {
    echo sprintf('<pre>%s</pre>',print_r($data,true));
}

print_r(…, true) returns the output without (yet) displaying it. From here it is inserted into the string using the printf function.


to break line with print_r:

echo "<pre>";
    print_r($lookup->query($_POST['zipcode']));
echo "</pre>";

The element will format it with any pre-existing formatting, so \n will turn into a new line, returned lines (when you press return/enter) will also turn into new lines.


https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre


You might need to add a linebreak:

echo $_POST['zipcode'] . '<br/>';

If you wish to add breaks between print_r() statements:

print_r($latitude); 
echo '<br/>';
print_r($longitude);

Tags:

Php