Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

\n vs. PHP_EOL vs. <br>?

Tags:

php

newline

nl2br

In most cases, as for one interactive website, when we output multiple lines of contents to web client browser, in my opinion, <BR /> is much more preferable than other two: \n or PHP_EOL.

Else, we need to use "<pre></pre>" to wrap the output content or use nl2br() to insert <BR /> before \n so as the multiple line mark can take effect in HTML. Like following example.

$fruits = array('a'=>'apple', 'b'=>'banana', 'c'=>'cranberry');  // Multiple lines by \n foreach( $fruits as $key => $value ){     echo "$key => $value \n" ; }  // Multiple lines by PHP_EOL reset( $fruits ); while ( list($key, $value) = each( $fruits ) ){     echo ("$key => $value" . PHP_EOL); }  // Multiple lines by <BR /> reset( $fruits ); while ( list($key, $value) = each( $fruits ) ){     echo ("$key => $value <BR />"); } 

Some people believe PHP_EOL is useful when writing data to a file, example a log file. It will create line breaks no matter whatever your platform.

Then, my question is when we use \n? What's the difference between \n and PHP_EOL, and <BR />? Could any body have a big list of each of their pros and cons?

like image 844
John Yin Avatar asked Jan 27 '14 05:01

John Yin


People also ask

What is Br in PHP?

Using Line Breaks as in HTML: The <br> tag in HTML is used to give the single line break. It is an empty tag, so it does not contain an end tag. Syntax: </br> Example: PHP.

What is PHP_ EOL?

The definition of PHP_EOL is that it gives you the newline character of the operating system you're working on.


2 Answers

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.

like image 126
Nick Coons Avatar answered Sep 20 '22 16:09

Nick Coons


<br>

<br> is only to be used when dividing up text. You may see this used for spacing out elements within an html document, but that is not its purpose, as layout spacing should be achieved with CSS such as margin. Here's a decent use case:

<p>This <br> is <br> some <br> text.</p> 

I typically break up my text with linebreaks for readability, so I wouldn't want the linebreaks rendered in the document. That being, the <br> tag is necessary.

CSS whitespace, <pre>

The html element <pre> is an element that, by default, uses CSS rule white-space: pre; so that the document's whitespace is rendered within that element. Normally, only a single space would be rendered regardless of the amount of whitespace in the document. A very good use of this style is for code blocks. If I write a code block, I will naturally space it as I intend and don't particularly care to wrap a <p> tag around each line (that's not semantic anyway) and I don't want to litter my code with <br> either. Even worse, there's the code indentation! whitespace: pre makes a lot of sense here.

<pre> function foo() {   //etc } </pre> 

\r, \n, PHP_EOL

Depending on the platform your php code is running from, you'll need to render a linebreak with \r, \n\, or both \r\n. PHP_EOL will automatically choose for you, so it is the most dynamic approach.

like image 27
m59 Avatar answered Sep 20 '22 16:09

m59