Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

php - vs. PHP_EOL vs. <br>?

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: or PHP_EOL.

Else, we need to use "<pre></pre>" to wrap the output content or use nl2br() to insert <BR /> before 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 

foreach( $fruits as $key => $value ){
    echo "$key => $value 
" ;
}

// 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 ? What's the difference between and PHP_EOL, and <BR />? Could any body have a big list of each of their pros and cons?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ) to represent a new line.

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

  • Mac (pre-OS X) - Uses a CR (that's ASCII 13, or ) 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 new line characters into <br> so that they provide line breaks in HTML display.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...