\ n vs. PHP_EOL vs.< br&gt ;? [英] \n vs. PHP_EOL vs. <br>?

查看:98
本文介绍了\ n vs. PHP_EOL vs.< br&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数情况下,就一个交互式网站而言,当我们向Web客户端浏览器输出多行内容时,我认为<BR />\nPHP_EOL其他两个更为可取.

否则,我们需要使用"<pre></pre>"来包装输出内容,或者使用nl2br()\n之前插入<BR />,这样多行标记才能在HTML中生效.像下面的例子一样.

$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 />");
}

有些人认为PHP_EOL在将数据写入文件(例如日志文件)时很有用.无论您使用什么平台,它都会创建换行符.

然后,我的问题是何时使用\n? \nPHP_EOL<BR />有什么区别?任何机构都可以列出各自的优缺点吗?

解决方案

DOS,Unix和Mac(在OS X和OS X之前的版本)都使用不同的字符或字符组合来表示转到下一行". /p>

  • DOS-使用CR + LF(ASCII 13,后接ASCII 10或\r\n)表示新行.

  • Unix-使用LF(ASCII 10或\n)表示新行.

  • Mac(OS X之前的版本)-使用CR(ASCII 13或\r)表示新行.

  • Mac(OS X)-与Unix一样,使用LF代表换行符.

因此,何时使用每一种取决于您要使用什么.如果您是为特定平台编写的,而不是为了移植性,请使用字符或字符组合来断开对该平台重要的行. PHP_EOL的目的是自动为平台选择正确的字符,以便您的新行与平台无关.

所有这些在浏览器中显示为一个空格,因为浏览器将空格折叠到显示空间中以进行显示(除非您如上所述使用<pre>或更改空格行为的CSS).正如您所提到的,这是<br>出现的地方,它将把这些\n新行字符转换为<br>,以便它们在HTML显示中提供换行符.

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?

解决方案

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.

这篇关于\ n vs. PHP_EOL vs.&lt; br&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆