PHP Echo 换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/255511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Echo Line Breaks
提问by PHLAK
What's the difference between \n and \r (I know it has something to do with OS), and what's the best way to echo a line break that will work cross platform?
\n 和 \r 之间有什么区别(我知道它与操作系统有关),回显可以跨平台工作的换行符的最佳方法是什么?
EDIT:In response to Jarod, I'll be using ths to echo a line break in a .txt log file, though I'm sure I'll be using it in the future for things such as echoing HTML makup onto a page.
编辑:作为对 Jarod 的回应,我将使用 ths 来回显 .txt 日志文件中的换行符,但我确信我将来会使用它来处理诸如将 HTML 制作回显到页面上的事情。
回答by Andrew Moore
Use the PHP_EOLconstant, which is automatically set to the correct line break for the operating system that the PHP script is running on.
使用PHP_EOL常量,它会自动设置为运行 PHP 脚本的操作系统的正确换行符。
Note that this constant is declared since PHP 5.0.2.
注意这个常量是从 PHP 5.0.2 开始声明的。
<?php
echo "Line 1" . PHP_EOL . "Line 2";
?>
For backwards compatibility:
为了向后兼容:
if (!defined('PHP_EOL')) {
switch (strtoupper(substr(PHP_OS, 0, 3))) {
// Windows
case 'WIN':
define('PHP_EOL', "\r\n");
break;
// Mac
case 'DAR':
define('PHP_EOL', "\r");
break;
// Unix
default:
define('PHP_EOL', "\n");
}
}
回答by Jarod Elliott
\nis a Linux/Unix line break.\ris a classic Mac OS (non-OS X) line break. Mac OS X uses the above unix\n.\r\nis a Windows line break.
\n是 Linux/Unix 换行符。\r是经典的 Mac OS(非 OS X)换行符。Mac OS X 使用上述 unix\n。\r\n是 Windows 换行符。
I usually just use \non our Linux systems and most Windows apps deal with it ok anyway.
我通常只\n在我们的 Linux 系统上使用,大多数 Windows 应用程序无论如何都可以处理它。
回答by Federico A. Ramponi
Jarod's answer contains the correct usage of \r \n on various OS's. Here's some history:
Jarod 的答案包含 \r \n 在各种操作系统上的正确用法。这是一些历史:
- \r, or the ASCII character with decimal code 13, is named CR after "carriage return".
- \n, or the ASCII character with decimal code 10, is named "newline", or LF after "line feed".
- \r 或十进制代码 13 的 ASCII 字符,以“回车”命名为 CR。
- \n 或十进制代码为 10 的 ASCII 字符被命名为“换行”,或“换行”后的 LF。
The terminology "carriage return" and "line feed" dates back to when teletypes were used instead of terminals with monitor and keyboard. With respect to teletypes or typewriters, "carriage return" meant moving the cursor and returning to the first column of text, while "line feed" meant rotating the roller to get onto the following line. At that time the distinction made sense. Today the combinations \n, \r, \r\n to represent the end of a line of text are completely arbitrary.
术语“回车”和“换行”可以追溯到使用电传打字机代替带有显示器和键盘的终端时。对于电传打字机或打字机,“回车”意味着移动光标并返回到文本的第一列,而“换行”意味着旋转滚轮以进入下一行。在那个时候,这种区分是有道理的。今天,代表一行文本结尾的组合 \n、\r、\r\n 是完全任意的。
回答by Chris Kunze
No backwards compatibility necessary for PHP_EOL on PHP4.
PHP4 上的 PHP_EOL 不需要向后兼容。
Need to correct Moore's statement on constant PHP_EOL availability: "... is declared since PHP 5.0.2.".
需要更正 Moore 关于持续 PHP_EOL 可用性的声明:“...自 PHP 5.0.2 起声明。”。
No, it has been around since PHP 4.3.10. Anyone who is still running anything lesser than that should not be in biz anyhow. As of today no one should be using anything lesser than PHP 5!
不,它从 PHP 4.3.10 开始就存在了。任何仍在运行低于此数量的东西的人无论如何都不应该进入商业领域。到今天为止,没有人应该使用低于 PHP 5 的任何东西!
来自 PHP 手册:“PHP_EOL 此平台的正确‘行尾’符号。自 PHP 4.3.10 和 PHP 5.0.2 起可用”。

