views:

6942

answers:

3

I'm pretty sure this has already been answered, but for the life of me I could not find the question or answer after browsing/searching StackOverflow for the last 20 minutes. Anyway, the question:

What's the difference between \n and \r (I know it has something to do with OS), and what's the best way to echo a line break that will work cross platform?

EDIT: In response to Jarod, I'll be using ths to echo a line break in a .txt log file, though I'm sure I'll be using it in the future for things such as echoing HTML makup onto a page.

+9  A: 

\n is a linux/unix line break

\r is a mac line break

\r\n is a windows line break

I usually just use \n on our linux systems and most windows apps deal with it ok anyway

Jarod Elliott
If I use \r\n will it work in Linux and in Windows? Also, will it output the \r as text in Linux if I do this?
PHLAK
Don't have access to linux right now to check but i'm pretty sure the \r doesn't come out properly in linux if you viewed it as a text file. Should be pretty easy to test the various options if you need to see what they look like.
Jarod Elliott
No it will not print, for ultimate compatibility, use \r\n
Unkwntech
Now that Mac OS X is Unix, does it use \n?
Imran
@Imran - good question! I was wondering that myself but don't have a Mac OS X to test with.
Jarod Elliott
+12  A: 

Jarod's answer contains the correct usage of \r \n on various OS's. Here's some history:

  • \r, or the ASCII character with decimal code 13, is named CR after "carriage return".
  • \n, or the ASCII character with decimal code 10, is named "newline", or LF after "line feed".

The terminology "carriage return" and "line feed" dates back to when teletypes were used instead of terminals with monitor and keyboard. With respect to teletypes or typewriters, "carriage return" meant moving the cursor and returning to the first column of text, while "line feed" meant rotating the roller to get onto the following line. At that time the distinction made sense. Today the combinations \n, \r, \r\n to represent the end of a line of text are completely arbitrary.

Federico Ramponi
+1 for the history which i didn't include :)
Jarod Elliott
Interesting history, thanks.
PHLAK
+9  A: 

Use the PHP_EOL constant, which is automatically set to the correct line break for the operating system that the PHP script is running on.

Note that this constant is declared since PHP 5.0.2.

<?php
    echo "Line 1" . PHP_EOL . "Line 2";
?>

For backwards compatibility:

if (!defined('PHP_EOL')) {
    switch (strtoupper(substr(PHP_OS, 0, 3))) {
        // Windows
        case 'WIN':
            define('PHP_EOL', "\r\n");
            break;

        // Mac
        case 'DAR':
            define('PHP_EOL', "\r");
            break;

        // Unix
        default:
            define('PHP_EOL', "\n");
    }
}
Andrew Moore
Not a bad idea, but I want to keep things as backwards compatible as possible.
PHLAK
Note that output isn't necessarily sent to the OS on which PHP is running.
eyelidlessness