46

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.

1
  • Further to my answer below, how are you using the echo'd output from php? Obviously in a web page the <br/> tag defines a line break so i assume this is being used differently? Nov 1, 2008 at 4:10

4 Answers 4

130

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");
    }
}
4
  • Not a bad idea, but I want to keep things as backwards compatible as possible.
    – PHLAK
    Nov 1, 2008 at 5:08
  • 9
    Note that output isn't necessarily sent to the OS on which PHP is running. Nov 1, 2008 at 5:50
  • 6
    This answer needs to be marked as the correct answer. I developed an app and tested it on Hostmonster.com (they run RedHat Linux) and the client deployed the code to a CentOS box. In dev "\n" rendered a newline as expected; on the CentOS box it didn't. Once I changed "\n" to PHP_EOL the issue went away and the code worked as expected. Lesson learned: even though the prod OS is a derivative of the dev OS, and even though "\n" should work on all *nix boxes, if it absolutely has to be implemented correctly then always use PHP_EOL, never "\n"
    – Mike C.
    Jan 4, 2011 at 20:38
  • Those that came here due to preg_match not detecting line ends, replace $ by (?:\r\n|\r|\n|$) (where ?: is a non-capturing regex group).
    – CPHPython
    Sep 3, 2018 at 17:07
49
  • \n is a Linux/Unix line break.
  • \r is a classic Mac OS (non-OS X) line break. Mac OS X uses the above unix \n.
  • \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.

6
  • 2
    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
    Nov 1, 2008 at 5:11
  • 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. Nov 1, 2008 at 5:54
  • 3
    No it will not print, for ultimate compatibility, use \r\n
    – UnkwnTech
    Nov 1, 2008 at 8:49
  • Now that Mac OS X is Unix, does it use \n?
    – Imran
    Nov 2, 2008 at 12:41
  • @Imran - good question! I was wondering that myself but don't have a Mac OS X to test with. Nov 2, 2008 at 22:09
31

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.

0
7

No backwards compatibility necessary for PHP_EOL on PHP4.

Need to correct Moore's statement on constant PHP_EOL availability: "... is declared since PHP 5.0.2.".

No, it has been around since PHP 4.3.10. Anyone who is still running anything lesser than that should not be in biz anyhow. As of today no one should be using anything lesser than PHP 5!

From the PHP manual: "PHP_EOL The correct 'End Of Line' symbol for this platform. Available since PHP 4.3.10 and PHP 5.0.2".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.