Jump to content

"\n" not working, arrays not displaying properly...


Recommended Posts

Hey everyone,

 

PHP is not co-operating today!

I'm having issues creating new lines...

 

This is my code:

 

<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);
echo "first line". "\n";
echo "second line\n";
echo "third line \n";
echo "...";
?>

 

The expected output is:

 

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)
first line
second line
third line
...

 

The actual output is:

 

Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) ) first line second line third line ...

 

Which is hardly readable when using large arrays!

Any help us much appreciated  :D

 

Thanks in advance!

 

 

 

If you view the source of the output you'll see it is as expected. You're looking for HTML <br /> line breaks. If you want the print_r to display the line breaks in html as well, use the second parameter in print_r to return the output and then perform nl2br on that to convert character \n to <br />.

 

echo nl2br(print_r($a, true));

As Daniel said, send the output as plain text rather than HTML and you won't have to play around with writing HTML tags to show the text in a monospace font.  Use something like the following, before your print_r:

 

header('Content-Type: text/plain; charset=utf-8');

 

 

Edit: Can't speel correctely!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.