PHP Echo Line Breaks
Asked Answered
M

4

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.

Mcclees answered 1/11, 2008 at 4:2 Comment(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?Photoneutron
P
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.

Photoneutron answered 1/11, 2008 at 4:4 Comment(6)
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?Mcclees
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.Photoneutron
No it will not print, for ultimate compatibility, use \r\nInterflow
Now that Mac OS X is Unix, does it use \n?Ardeha
@Ardeha - good question! I was wondering that myself but don't have a Mac OS X to test with.Photoneutron
Yes, Mac OS X uses \n as well. \r is completely obsolete now.Baeyer
I
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");
    }
}
Instep answered 1/11, 2008 at 4:25 Comment(4)
Not a bad idea, but I want to keep things as backwards compatible as possible.Mcclees
Note that output isn't necessarily sent to the OS on which PHP is running.Naga
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"Jurisprudence
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).Daugherty
P
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.

Photoneutron answered 1/11, 2008 at 4:4 Comment(6)
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?Mcclees
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.Photoneutron
No it will not print, for ultimate compatibility, use \r\nInterflow
Now that Mac OS X is Unix, does it use \n?Ardeha
@Ardeha - good question! I was wondering that myself but don't have a Mac OS X to test with.Photoneutron
Yes, Mac OS X uses \n as well. \r is completely obsolete now.Baeyer
G
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.

Grogshop answered 1/11, 2008 at 4:23 Comment(0)
J
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".

Jimmy answered 13/4, 2012 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.