\N VS. PHP_Eol VS. <Br>

\n vs. PHP_EOL vs. br?

DOS, Unix, and Mac (pre-OS X and OS X) all use different characters or character combinations to represent "go to the next line."

  • DOS - Uses a CR+LF (that's ASCII 13 followed by an ASCII 10, or \r\n) to represent a new line.

  • Unix - Uses an LF (that's ASCII 10, or \n) to represent a new line.

  • Mac (pre-OS X) - Uses a CR (that's ASCII 13, or \r) to represent a new line.

  • Mac (OS X) - Like Unix, uses an LF to represent a new line.

Therefore, when to use each one depends on what you're going for. If you're writing for a specific platform without the intention of portability, use the character or character combination to break lines that matter to that platform. The purpose of PHP_EOL is to automatically choose the correct character for the platform, so that your new lines are platform-independent.

All of these appear as a single space within a browser as browsers collapse whitespace into a display space for display purposes (unless you're using <pre> as you mentioned, or CSS that changes the behavior of whitespace). This is where <br> comes in, as you've mentioned, which will convert these \n new line characters into <br> so that they provide line breaks in HTML display.

The difference between \n and br / in php

<br /> is a HTML line-break, whereas \n is a newline character in the source code.

In other words, <br /> will make a new line when you view the page as rendered HTML, whereas \n will make a new line when you view the source code.

Alternatively, if you're outputting to a console rather than somewhere that will be rendered by a web browser then \n will create a newline in the console output.

What is the benefit of \n and PHP_EOL in PHP?

A web browser interprets the output of a PHP program as HTML, so \n and \r\n will not appear to do anything, just like inserting a newline in an HTML file. On the other hand, <br /> makes a new line in the interpreted HTML (hence "line BReak"). Therefore, <br /> will make new lines, whereas \r\n will not do anything.

why does PHP_EOL write \r\n at the end of each line

Without the double quotes, PHP doesn't process the special character and should replace as you expect.

$line = str_replace('\r\n',"<br/>",$line); // note '\r\n' not "\r\n"

When do I use PHP_EOL instead of \n and vice-versa ? Ajax/Jquery client problem

The constant PHP_EOL should generally be used for platform-specific output.

  • Mostly for file output really.
  • Actually the file functions already transform \n ←→ \r\n on Windows systems unless used in fopen(…, "wb") binary mode.

For file input you should prefer \n however. While most network protocols (HTTP) are supposed to use \r\n, that's not guaranteed.

  • Therefore it's best to break up on \n and remove any optional \r manually:

    $lines = array_map("rtrim", explode("\n", $content));

    Or use the file(…, FILE_IGNORE_NEW_LINES) function right away, to leave EOL handling to PHP or auto_detect_line_endings.

  • A more robust and terser alternative is using preg_split() and a regexp:

    $lines = preg_split("/\R/", $content);

    The \R placeholder detects any combination of \r + \n. So would be safest, and even work for Classic MacOS ≤ 9 text files (rarely seen in practice).

    Obligatory microoptimization note:

    While regex has a cost, it's surprisingly often speedier than manual loops and string postprocessing in PHP.

And there are a few classic examples where you should avoid PHP_EOL due to its platform-ambiguity:

  • Manual generation of network protocol payloads, such as HTTP over fsockopen().
  • For mail() and MIME construction (which really, you shouldn't do tediously yourself anyway).
  • File output, if you want to consistently write just Unix \n newlines regardless of environment.

So use a literal "\r\n" combination when not writing to files, but preparing data for a specific context that expects network linebreaks.

What is the benefit of \n and PHP_EOL in PHP?

A web browser interprets the output of a PHP program as HTML, so \n and \r\n will not appear to do anything, just like inserting a newline in an HTML file. On the other hand, <br /> makes a new line in the interpreted HTML (hence "line BReak"). Therefore, <br /> will make new lines, whereas \r\n will not do anything.

When do I use the PHP constant PHP_EOL?

Yes, PHP_EOL is ostensibly used to find the newline character in a cross-platform-compatible way, so it handles DOS/Unix issues.

Note that PHP_EOL represents the endline character for the current system. For instance, it will not find a Windows endline when executed on a unix-like system.

PHP Echo Line Breaks

  • \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.



Related Topics



Leave a reply



Submit