Reputation: 181
What are these called? \r
, \n
Is there a tutorial that explains them?
Upvotes: 17
Views: 86787
Reputation: 762
"\r" is Carriage return. Mind the double quotes when you use it with PHP! If you use single quotes, You will get the string \r instead of a line break.
BTW, it is suitable to use "\r\n" if you want to put a line break in your text so it will be rendered correctly in different operating systems.
Upvotes: 3
Reputation: 126
\n
is the newline or linefeed, other side \r
is the carriage return. They differ in what uses them. Windows uses \r\n
to signify the enter key was pressed, while Linux and Unix use \n
to signify that the enter key was pressed.
In Unix and all Unix-like systems, \n
is the code for end-of-line, \r
means nothing special. In old Mac systems (pre-OS X), \r
was the code for end-of-line instead. \r\n
is the standard line-termination for text formats on the Internet.
\r
commands the carriage to go back leftwards until it hits the leftmost stop, \n
commands the roller to roll up one line (a much faster operation). That's the reason you always have \r before \n, so that the roller can move while the carriage is still going leftwards.
Upvotes: 5
Reputation: 163248
\r
is the carriage return
\n
is the newline
These are available in many other languages aside from PHP.
Upvotes: 16
Reputation: 1897
Not an answer to your question, but relevant nonetheless: I'd recommend using the PHP_EOL constant whenever you want to insert a new line. The PHP_EOL constant contains the correct new line character(s) for the platform on which the script is being run (\n on Unix, \r\n on Windows).
Upvotes: 6
Reputation: 3248
\r
is a Carriage Return
\n
is a Line Feed (or new line).
On Windows systems these together make a newline (i.e. every time you press the enter button your fix will get a \r\n
).
In PHP if you open a Windows style text file you will get \r\n
at the end of paragraphs / lines were you've hit enter. If it was a Unix style text file you'd only get a \n
.
Upvotes: 5
Reputation: 1500585
They're "carriage return" and "line feed" respectively. Typically on Windows, you need both together to represent a line terminator: "\r\n" whereas on most (all?) Unix systems, "\n" is enough.
See the Wikipedia Newline entry for more details about the vagaries of different systems.
See the PHP manual for more details about escape sequences in general, and the other ones available in PHP.
Many other languages (e.g. C, C++, C#, Java, Perl, Python, Ruby) share the same escape sequences for carriage return and line feed - but they are all specified for the individual language. (In other words, it is language specific, but the answer would be the same for many languages.)
Upvotes: 27
Reputation: 186562
They're escape sequences. \n
is a newline and \r
is a carriage return.
In Windows most text editors have a newline as \r\n and on unix it's \n
Upvotes: 8