Helen Neely
Helen Neely

Reputation: 4740

Print PHP string in new lines

Anything wrong with this code? I want it to print the name and address - each on a separate line, but it all comes up in one line.

Here's the code

<?php 

    $myname = $_POST['myname'];
    $address1 = $_POST['address1'];
    $address2 = $_POST['address2'];
    $address3 = $_POST['address3'];
    $town = $_POST['town'];
    $county = $_POST['county'];

    $content = '';

        $content .="My name    = " .$myname ."\r\n";
        $content .="Address1   = " .$address1 ."\n";
        $content .="Address2   = " .$address2 ."\n"; 
        $content .="Address3   = " .$address3 ."\n"; 
        $content .="town       = " .$town ."\n"; 
        $content .="county     = " .$county ."\n"; 

        echo $content;

?>

It looks like the '\n' character is not working.

Upvotes: 1

Views: 7602

Answers (2)

Jules
Jules

Reputation: 7213

In your source code this will show on a next line, but if you want to go to another line in HTML you will have to append <br />.

So:

$content .="My name    = " .$myname ."<br />\r\n";

I left the \r\n here because it will go to the next line in your source code aswell, which might look nicer if you have to view the source.

Upvotes: 5

kba
kba

Reputation: 19466

The \n character properly works just fine. The problem is, it's not what you expect.

If you see this in a browser, you won't see line breaks, because line breaks are ignored in the source code. The HTML parser only reads <br> as line breaks.

If you try to go to your website and view the source code, you'll find that the line breaks are in there.

Upvotes: 3

Related Questions