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?
while
construction with simplyforeach($fruits as $key => $value)
. That way you also don't have toreset($fruits)
. – Abernon<br>
is for a whole different context than a line break in a simple text file. – Uncanny"\n"
if you're guaranteed on unix/linux, and not in HTML mode (2)PHP_EOL
if you're not inHTML
mode (3)<br>
if you're explicitly and surely outputtingHTML
(4) if you're not sure whether html is desired or not, usingnl2br()
later on when you're sure is easier then removing<br>
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. – Cerebrum