php PHP中的\r和\n是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3274743/
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
What are \r and \n meaning in PHP?
提问by categorizes
what are these called \r \n and is there a tutorial that explains them?
这些被称为\r\n 是什么,是否有解释它们的教程?
回答by Jon Skeet
They're "carriage return" and "line feed" respectively. Typically on Windows, you need both together to represent a line terminator: "\r\n" whereas on most (all?) Unix systems, "\n" is enough.
它们分别是“回车”和“换行”。通常在 Windows 上,您需要将两者一起表示行终止符:“\r\n”,而在大多数(所有?)Unix 系统上,“\n”就足够了。
See the Wikipedia Newline entryfor more details about the vagaries of different systems.
有关不同系统的变幻莫测的更多详细信息,请参阅Wikipedia Newline 条目。
See the PHP manualfor more details about escape sequences in general, and the other ones available in PHP.
有关一般转义序列以及 PHP 中可用的其他转义序列的更多详细信息,请参阅PHP 手册。
Many other languages (e.g. C, C++, C#, Java, Perl, Python, Ruby) share the same escape sequences for carriage return and line feed - but they are all specified for the individual language. (In other words, it islanguage specific, but the answer would be the same for many languages.)
许多其他语言(例如 C、C++、C#、Java、Perl、Python、Ruby)共享用于回车和换行的相同转义序列 - 但它们都是为个别语言指定的。(换句话说,它是特定于语言的,但许多语言的答案都是一样的。)
回答by Jacob Relkin
\ris the carriage return
\r是回车
\nis the newline
\n是换行符
These are available in many other languages aside from PHP.
除了 PHP 之外,这些还可以在许多其他语言中使用。
回答by meder omuraliev
They're escape sequences. \nis a newline and \ris a carriage return.
它们是转义序列。\n是换行符,\r是回车符。
In Windows most text editors have a newline as \r\n and on unix it's \n
在 Windows 中,大多数文本编辑器都有一个换行符 \r\n 而在 unix 上它是 \n
回答by Victor Welling
Not an answer to your question, but relevant nonetheless: I'd recommend using the PHP_EOL constant whenever you want to insert a new line. The PHP_EOL constant contains the correct new line character(s) for the platform on which the script is being run (\n on Unix, \r\n on Windows).
不是您的问题的答案,但仍然相关:我建议您在想要插入新行时使用 PHP_EOL 常量。PHP_EOL 常量包含运行脚本的平台的正确换行符(\n 在 Unix 上,\r\n 在 Windows 上)。
回答by Oren
\ris a Carriage Return\nis a Line Feed (or new line).
On Windows systems these together make a newline (i.e. every time you press the enter button your fix will get a \r\n).
在 Windows 系统上,这些一起构成一个换行符(即每次按下回车按钮时,您的修复都会得到一个\r\n)。
In PHP if you open a Windows style text file you will get \r\nat the end of paragraphs / lines were you've hit enter. If it was a Unix style text file you'd only get a \n.
在 PHP 中,如果您打开一个 Windows 样式的文本文件,您将\r\n在按 Enter 键的段落/行的末尾看到。如果它是一个 Unix 风格的文本文件,你只会得到一个\n.
回答by Web7
\nis the newline or linefeed, other side \ris the carriage return. They differ in what uses them. Windows uses \r\nto signify the enter key was pressed, while Linux and Unix use \nto signify that the enter key was pressed.
\n是换行符或换行符,另一边\r是回车。它们的用途不同。Windows 用于\r\n表示按下了回车键,而 Linux 和 Unix 用于\n表示按下了回车键。
In Unix and all Unix-like systems, \nis the code for end-of-line, \rmeans nothing special. In old Mac systems (pre-OS X), \rwas the code for end-of-line instead. \r\nis the standard line-termination for text formats on the Internet.
在 Unix 和所有类 Unix 系统中,\n是行尾的代码,\r没有什么特别的意思。在旧的 Mac 系统(OS X 之前)中,\r是行尾代码。\r\n是 Internet 上文本格式的标准行终止符。
\rcommands the carriage to go back leftwards until it hits the leftmost stop, \ncommands the roller to roll up one line (a much faster operation). That's the reason you always have \r before \n, so that the roller can move while the carriage is still going leftwards.
\r命令小车向左后退,直到碰到最左边的停止点,\n命令滚轮卷起一行(一个更快的操作)。这就是你总是在 \n 之前有 \r 的原因,这样滚轮可以在托架仍然向左移动时移动。
回答by Dushan
"\r" is Carriage return. Mind the double quotes when you use it with PHP! If you use single quotes, You will get the string \rinstead of a line break.
“\r”是回车。在 PHP 中使用时请注意双引号!如果您使用单引号,您将得到字符串\r而不是换行符。
BTW, it is suitable to use "\r\n"if you want to put a line break in your text so it will be rendered correctly in different operating systems.
顺便说一句,如果您想在文本中放置换行符,则适合使用"\r\n"以便在不同的操作系统中正确呈现。
- Mac: \r
- Linux/Unix: \n
- Windows: \r\n
- 苹果机:\r
- Linux/Unix:\n
- 视窗:\r\n

