ssvarc
ssvarc

Reputation: 371

How do I display print_r on different lines?

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

Upvotes: 26

Views: 78672

Answers (5)

charelf
charelf

Reputation: 3825

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

Upvotes: 9

Manngo
Manngo

Reputation: 16381

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.

Upvotes: 3

Sen Sokha
Sen Sokha

Reputation: 1902

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

Upvotes: 22

span
span

Reputation: 5624

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);

Upvotes: 32

Jigar Tank
Jigar Tank

Reputation: 1784

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

Upvotes: 2

Related Questions