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?
` is for a whole different context than a line break in a simple text file. – CBroe Jan 27 '14 at 05:21
` if you're explicitly and surely outputting `HTML` (4) if you're not sure whether html is desired or not, using `nl2br()` later on when you're sure is easier then removing `
` later (5) actually, one should _not output anything if the output format is not known yet_, which could mean you'd rather return arrays then strings with an uncertain seperator, postponing the decision how to delimit it a string until you _do_ know what to use. – Wrikken Jan 27 '14 at 05:22