Asked  7 Months ago    Answers:  5   Viewed   29 times

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?

 Answers

69

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 rn) 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.

Wednesday, March 31, 2021
 
cegfault
answered 7 Months ago
44

Use nl2br() to turn the new lines into HTML <br/> elements:

<?php

echo nl2br("Hello World!nSecond line");

?>    
Wednesday, March 31, 2021
 
PHLAK
answered 7 Months ago
84
$numberNewline = $number . "n";
fwrite($file, $numberNewline);

Try this

Saturday, May 29, 2021
 
Marcelo
answered 5 Months ago
53

Evaluate. The criterion for choosing the representation is whatever works.

Indeed, character level (!= bytes, unless you only care about english) probably is the most common representation, because it is robust to spelling differences (which do not need to be errors, if you look at history; spelling changes). So for spelling correction purposes, this works well.

On the other hand, Google Books n-gram viewer uses word level n-grams on their books corpus. Because they don't want to analyze spelling, but term usage over time; e.g. "child care", where the individual words aren't as interesting as their combination. This was shown to be very useful in machine translation, often referred to as "refrigerator magnet model".

If you are not processing international language, bytes may be meaningful, too.

Saturday, August 14, 2021
 
ripper234
answered 3 Months ago
67

How about HTML/CSS? If you put your text inside a <pre> tag, it will show all newlines exactly as they were. Alternatively, you can achieve the same effect by applying the CSS style white-space:pre to any element.

Don't forget to HTMLencode it still (< to &lt; etc.), otherwise it will all break apart at the first angle bracket.

Saturday, September 18, 2021
 
adizone
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :