26

When I run the following code:

echo $_POST['zipcode'];

print_r($lookup->query($_POST['zipcode']));

?>

the results are concatenated on one line like so: 10952Array.

How can I get it to display on separate lines, like so:

08701
Array

5 Answers 5

32

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);
2
  • 1
    And when it's two print_r statements how do I add the linebreak? print_r($latitude); print_r($longitude);
    – ssvarc
    Dec 15, 2011 at 7:20
  • You can add an extra echo '<br/>' in between the print_r() statements. See edit.
    – span
    Dec 15, 2011 at 7:22
21

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

1
9

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>";
3

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.

2
  • Lovely solution. Thank you.
    – carloswm85
    Jul 8, 2021 at 21:07
  • Here I contribute with some classic black and green console styling. Enclose the <pre> tag with <div> tag, and add the following rule to your CSS: pre { padding: 1em; background-color: #000000; color: #08FF08; }
    – carloswm85
    Jul 9, 2021 at 15:45
2

Just echo these : echo $_POST['zipcode']."<br/>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.