Forum Moderators: coopster
AFAIK,
\n means "newline" and \r means "carriage return" (what you get when you hit the Enter key while typing.) \r is typically only used by Windows implementations (i.e. running PHP on a Windows server) when it can't figure out what \n means. On a Windows server, you can expect the
\n newline code to be ignored and the \r\n carriage return PLUS newline to break the line once. Linux servers will ignore the \r carriage return when there is a \n newline present. Testing on an Apache server running on Linux:
<? echo "<pre>\n"; echo "New\nline<br />\n"; echo "Carriage\rreturn<br />\n"; echo "Both\r\ntypes<br />\n"; echo "</pre>\n"; ?> gives:
New line Carriage return Both types illustrating that the Linux implementation of PHP does indeed ignore the
\r when the \n is present. Two \n\n produces a double linebreak, as expected. [edited by: StupidScript at 10:55 pm (utc) on Aug. 11, 2006]
On the Linux platform, the
\r should be ignored when there is a \n present. That's what my quick test proved. Are you outputting to a text file, and that's where you are seeing the result?
I just used preformatted HTML (
<pre>) to view it in my browsers (FF and MSIE both show the same result). Using
\r\n is just a way of coding so that if you move from a Linux box to a Windows box the code will work the same. Windows pays attention to the \r and Linux pays attention to the \n, OR a \r without an \n. [edited by: StupidScript at 11:09 pm (utc) on Aug. 11, 2006]
Multiple types of line breaks have been placed within this string See? AND, I see what is going on ... just like I noted above, PLUS I learned something new, although I don't know how reliable it is, yet. ;)
The first break:
\r Linux PHP is using it because there is no accompanying
\n. The second pair of breaks:
\n\r Linux PHP is using the
\n and then ALSO using the \r because there is no accompanying \n. Hang on ... the next one will explain it to you, I think ... The third pair of breaks:
\r\n Linux PHP is ignoring the
\r because there IS an accompanying \n, which it is using. The last pair of breaks:
\n\n Linux PHP is using both of them.
The correct order is
\r accompanied by \n, not the other way around. It's a "reading left-to-right" thing. \n FINISHES the instruction, so if there is a \r following it, that's a new instruction. The part that I learned is that a normal Enter-type line break in the middle of an echo statement will ALSO produce a visible line break on a Linux PHP installation. Frankly I thought it would throw an error. I don't know if that can be counted on, though, so I'll keep using
\r\n just to be safe.