30

I have the following code:

$message = "Good news! The item# $item_number on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\n
The seller, $bid_user is making this offer.\n
\nItem Title : $title\n

\nAll the best,\n
$bid_user\n
$email\n
";

echo $message;
$htmlContent = $baseClass->email_friend($item_number, $title, $bid_price, $bid_user, $mcat, $message, $email, $VIEWSTATE, $EVENTVALIDATION, $URL);

The problem is that the new line break (\n) is not working.

Brendan Bullen
  • 11,607
  • 1
  • 31
  • 40
Michael
  • 6,377
  • 14
  • 59
  • 91
  • 3
    That isn't even a PHP string. It's just plain text. – BoltClock Jul 08 '10 at 18:01
  • 1
    show us some more sample code, and how do you want to output this? in HTML? Then it's clear why they're not working. if you don't use the `
    ` tag every whitespace (\n\r\t) will be ineterpreted as just a space, and multiple white spaces in succession will be interpreted as one
    – jigfox Jul 08 '10 at 18:03
  • I need to send this as text to a message system which doesn't support html – Michael Jul 08 '10 at 18:04
  • Does the message system support new lines and if yes, how? – jeroen Jul 08 '10 at 18:09
  • 3
    If the system expects Win-style newline, `\n` won't work, you will need to add `\r\n` for it to happen – Ast Derek Jul 08 '10 at 18:30

8 Answers8

47

Try \r\n in place of \n

The difference between \n and \r\n

It should be noted that this is applicable to line returns in emails. For other scenarios, please refer to rokjarc's answer.

Community
  • 1
  • 1
Brendan Bullen
  • 11,607
  • 1
  • 31
  • 40
  • 2
    New lines in plain text emails should always be CRLF ("\r\n"): [RFC 2822](http://www.faqs.org/rfcs/rfc2822.html) – Chris Williams Mar 15 '11 at 18:04
  • Thank you, if you put it in `
    ` tags the newlines are also shown in HTML.
    – ph3nx Feb 26 '13 at 16:24
  • 1
    @jberger So you downvoted this because you felt another answer was better? That does not mean this answer isn't useful (The definition of a downvote). That and I don't believe `PHP_EOL` is the correct solution here - this answer is for outputting email content which will not be rendered on the source machine producing the end of line character. Therefore basing it off that platform is incorrect. As Chris Williams pointed out above, the RFC dictates that it has to be `\r\n`. Try this answer: http://stackoverflow.com/questions/3248090/should-php-eol-be-used-in-emails – Brendan Bullen Sep 04 '13 at 09:11
  • 1
    @BrendanBullen the question title is **PHP new line break**. You are correct that this is for email content. The question title should probably be **PHP new line break in email**. Please edit your answer, so I can clear my downvote. (stackoverflow has locked it.) – Jake Berger Sep 04 '13 at 16:24
  • @jberger I think you're right. I've updated the answer to clear up any future confusion and updated the question title. – Brendan Bullen Sep 05 '13 at 14:13
31

I know this is an old question but anyway it might help someone.

I tend to use PHP_EOL for this purposes (due to cross-platform compatibility).

echo "line 1".PHP_EOL."line 2".PHP_EOL;

If you're planning to show the result in a browser then you have to use "<br>".

EDIT: since your exact question is about emails, things are a bit different. For pure text emails see Brendan Bullen's accepted answer. For HTML emails you simply use HTML formatting.

Community
  • 1
  • 1
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
  • 1
    I've looked into this and I don't believe this is the best way of approaching this due to the fact that it's an email. The explanation here seems to make sense: http://stackoverflow.com/questions/3248090/should-php-eol-be-used-in-emails – Brendan Bullen Sep 04 '13 at 09:17
  • 1
    @BrendanBullen: you've got a valid point here. I came to this page searching for "linebreak in PHP" as probably most of the others when looking for answer. The fact that this is about email is well hidden inside the posted code. – Rok Jarc Sep 04 '13 at 11:01
  • I think most are wanting to show the line break in the browser. Thanks! – Joshua Dance Jan 16 '14 at 17:54
21

Are you building this string using single or double quotes? \r and \n only work with double quotes, as well as embedded variables. For example:

$foo = 'bar';
echo 'Hello \n $foo!';

will output:

Hello \n $foo!

But:

$foo = 'bar';
echo "Hello \n $foo!";

will output:

Hello
bar!
Harold1983-
  • 3,329
  • 2
  • 23
  • 22
7

you can use "Line1<br>Line2"

Amjad Abu Saa
  • 1,644
  • 2
  • 15
  • 11
6

If you output to html or an html e-mail you will need to use <br> or <br /> instead of \n.

If it's just a text e-mail: Are you perhaps using ' instead of "? Although then your values would not be inserted either...

jeroen
  • 91,079
  • 21
  • 114
  • 132
4

EDIT: Maybe your class for sending emails has an option for HTML emails and then you can use <br />

1) Double-quotes

$output = "Good news! The item# $item_number  on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\nThe seller, $bid_user is making this offer.\n\nItem Title : $title\n\nAll the best,\n $bid_user\n$email\n";

If you use double-quotes then \n will work (there will be no newline in browser but see the source code in your browser - the \n characters will be replaced for newlines)

2) Single quotes doesn't have the effect as the double-quotes above:

$output = 'Good news! The item# $item_number  on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\nThe seller, $bid_user is making this offer.\n\nItem Title : $title\n\nAll the best,\n $bid_user\n$email\n';

all characters will be printed as is (even variables!)

3) Line breaks in HTML

$html_output = "Good news! The item# $item_number  on which you placed a bid of <br />$ $bid_price is now available for purchase at your bid price.<br />The seller, $bid_user is making this offer.<br /><br />Item Title : $title<br /><br />All the best,<br /> $bid_user<br />$email<br />";
  • There will be line breaks in your browser and variables will be replaced with their content.
MartyIX
  • 27,828
  • 29
  • 136
  • 207
2

When we insert any line break with a programming language the char code for this is "\n". php does output that but html can't display that due to htmls line break is
. so easy way to do this job is replacing all the "\n" with "
". so the code should be

str_replace("\n","<br/>",$str);

after adding this code you wont have to use pre tag for all the output oparation.

copyed this ans from this website :

2

if you are outputting the code as html - change /n -->
and do echo $message;

m0g
  • 969
  • 2
  • 15
  • 30