d-_-b
d-_-b

Reputation: 23161

difference between \r\n and \n\n in php email

I'm setting up a php mail sending script...

While adding different headers, I've come across the symbol for a new line in the email (since a < br > tag won't do)...

So my question is what is the difference between \r\n and \n\n ?

for example:

echo $var1 . " \r\n" . $var2 . "\n\n";

thanks for your help!

Upvotes: 3

Views: 9286

Answers (7)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

\r\n is the standard.

From RFC 2822

Messages are divided into lines of characters. A line is a series of characters that is delimited with the two characters carriage-return and line-feed; that is, the carriage return (CR) character (ASCII value 13) followed immediately by the line feed (LF) character (ASCII value 10). (The carriage-return/line-feed pair is usually written in this document as "CRLF".)

The other \n\n is just 2 lines.

If this is the email headers I believe it would be

echo $var1 . " \r\n" . $var2 . "\r\n\r\n";

Here \r\n\r\n is the delimiter of header and body. It could be HTTP or SMTP

Upvotes: 7

deex
deex

Reputation: 515

"\r\n" is primarily used in DOS, while in UNIX it is express in "\n". So if you interpret "\n\n" in UNIX it will be equivalent to two line break. Since you mention PHP, it's safe to use PHP predefined constant PHP_EOL.

Upvotes: 1

Andrew Jackman
Andrew Jackman

Reputation: 13966

\r means carriage return and \n means new line. Its easier to think about it like a type writer, the \r is when you push the paper holder (maybe it is called a carriage???) from the left to right, so you can type at the beginning of the line, and the \n will lower the line by one. Traditionally windows and windows based programs required both, but most programs now recognize \n as a new line for all operating systems.

Upvotes: 4

Jack
Jack

Reputation: 5768

\r carriage return moves the input position back to the beginning. Then \n advances to the new line.

Upvotes: 0

Benjamin Schulte
Benjamin Schulte

Reputation: 873

Actually \r\n is the windows standard for a single line break. The unix standard is a single \n character.

Mostly (in emails) you can ignore the \r character and only use \n for a single line break. Which also means that \n\n represents a double line break.

Upvotes: 2

dan-lee
dan-lee

Reputation: 14492

\r\n is a new line in windows and \n\n would be two new lines on UNIX like systems.

Don't confuse a new line with a line break, which exists in HTML. It's basically just a new line you would see in a plain text file but not in browsers.

See Newline

Upvotes: 2

Jared
Jared

Reputation: 406

\n is a new line and \r is a carriage return.

http://en.wikipedia.org/wiki/Newline#In_programming_languages

Upvotes: 0

Related Questions