What are these called? \r
, \n
Is there a tutorial that explains them?
mkrieger1
15.7k4 gold badges46 silver badges57 bronze badges
asked Jul 18, 2010 at 7:52
1
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.]
answered Jul 18, 2010 at 7:53
Jon SkeetJon Skeet
1.4m835 gold badges8985 silver badges9091 bronze badges
\r
is the carriage return
\n
is the newline
These are available in many other languages aside from PHP.
answered Jul 18, 2010 at 7:53
Jacob RelkinJacob Relkin
158k32 gold badges341 silver badges318 bronze badges
0
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
answered Jul 18, 2010 at 7:53
meder omuralievmeder omuraliev
180k70 gold badges383 silver badges427 bronze badges
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].
answered Jul 18, 2010 at 7:58
\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
.
answered Jul 18, 2010 at 7:56
OrenOren
3,2432 gold badges18 silver badges15 bronze badges
\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.
answered May 31, 2017 at 4:31
Web7Web7
11612 bronze badges
"\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.
- Mac: \r
- Linux/Unix: \n
- Windows: \r\n
answered May 17, 2018 at 7:52
DushanDushan
5926 silver badges13 bronze badges