什么时候使用 PHP 常量“PHP_EOL”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/128560/
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
When do I use the PHP constant "PHP_EOL"?
提问by Christian Oudard
采纳答案by Adam Bellaire
Yes, PHP_EOLis ostensibly used to find the newline character in a cross-platform-compatible way, so it handles DOS/Unix issues.
是的,PHP_EOL表面上用于以跨平台兼容的方式查找换行符,因此它可以处理 DOS/Unix 问题。
Note that PHP_EOLrepresents the endline character for the currentsystem. For instance, it will not find a Windows endline when executed on a unix-like system.
注意PHP_EOL代表当前系统的结束符。例如,在类 Unix 系统上执行时,它不会找到 Windows 端线。
回答by AlexV
From main/php.hof PHP version 7.1.1 and version 5.6.30:
从main/php.hPHP 7.1.1 版和 5.6.30 版开始:
#ifdef PHP_WIN32
# include "tsrm_win32.h"
# include "win95nt.h"
# ifdef PHP_EXPORTS
# define PHPAPI __declspec(dllexport)
# else
# define PHPAPI __declspec(dllimport)
# endif
# define PHP_DIR_SEPARATOR '\'
# define PHP_EOL "\r\n"
#else
# if defined(__GNUC__) && __GNUC__ >= 4
# define PHPAPI __attribute__ ((visibility("default")))
# else
# define PHPAPI
# endif
# define THREAD_LS
# define PHP_DIR_SEPARATOR '/'
# define PHP_EOL "\n"
#endif
As you can see PHP_EOLcan be "\r\n"(on Windows servers) or "\n"(on anything else). On PHP versions prior5.4.0RC8, there were a third value possible for PHP_EOL: "\r"(on MacOSX servers). It was wrong and has been fixed on 2012-03-01 with bug 61193.
如您所见,PHP_EOL可以是"\r\n"(在 Windows 服务器上)或"\n"(在其他任何服务器上)。在5.4.0RC8之前的PHP 版本上,可能有第三个值PHP_EOL:("\r"在 MacOSX 服务器上)。这是错误的,已于 2012-03-01 以错误 61193 修复。
As others already told you, you can use PHP_EOLin any kind of output (where anyof these values are valid - like: HTML, XML, logs...) where you want unified newlines. Keep in mind that it's the server that it's determining the value, not the client. Your Windows visitors will get the value from your Unix server which is inconvenient for them sometimes.
正如其他人已经告诉您的那样,您可以PHP_EOL在需要统一换行符的任何类型的输出中使用(这些值中的任何一个都有效 - 例如:HTML、XML、日志...)。请记住,确定值的是服务器,而不是客户端。您的 Windows 访问者将从您的 Unix 服务器获得价值,这有时对他们来说很不方便。
I just wanted to show the possibles values of PHP_EOLbacked by the PHP sources since it hasn't been shown here yet...
我只是想显示PHP_EOLPHP 源支持的可能值,因为这里还没有显示......
回答by Zoredache
You use PHP_EOLwhen you want a new line, and you want to be cross-platform.
您可以使用PHP_EOL,当你想要一个新的生产线,和你想成为跨平台的。
This could be when you are writing files to the filesystem (logs, exports, other).
这可能是在您将文件写入文件系统(日志、导出、其他)时。
You could use it if you want your generated HTML to be readable. So you might follow your <br />with a PHP_EOL.
如果您希望生成的 HTML 可读,则可以使用它。所以你可能会跟随你<br />的PHP_EOL.
You would use it if you are running php as a script from cron and you needed to output something and have it be formatted for a screen.
如果您从 cron 将 php 作为脚本运行,并且您需要输出某些内容并将其格式化为屏幕,则您将使用它。
You might use it if you are building up an email to send that needed some formatting.
如果您正在构建一封电子邮件以发送需要一些格式的电子邮件,您可能会使用它。
回答by Salman A
PHP_EOL (string) The correct 'End Of Line' symbol for this platform. Available since PHP 4.3.10 and PHP 5.0.2
PHP_EOL(字符串)此平台的正确“行尾”符号。自 PHP 4.3.10 和 PHP 5.0.2 起可用
You can use this constant when you read or write text files on the server's filesystem.
当您在服务器的文件系统上读取或写入文本文件时,您可以使用此常量。
Line endings do not matter in most cases as most software are capable of handling text files regardless of their origin. You ought to be consistent with your code.
在大多数情况下,行尾无关紧要,因为大多数软件都能够处理文本文件,而不管它们的来源如何。您应该与您的代码保持一致。
If line endings matter, explicitly specify the line endings instead of using the constant. For example:
如果行尾很重要,请明确指定行尾而不是使用常量。例如:
- HTTP headers mustbe separated by
\r\n - CSV files shoulduse
\r\nas row separator
- HTTP 标头必须由
\r\n - CSV文件应该使用
\r\n作为行分隔符
回答by Iain Collins
I'd like to throw in an answer that addresses "When notto use it" as it hasn't been covered yet and can imagine it being used blindly and no one noticing the there is a problem till later down the line. Some of this contradicts some of the existing answers somewhat.
我想提出一个解决“何时不使用它”的答案,因为它还没有被涵盖,并且可以想象它被盲目使用并且直到后来才注意到有问题。其中一些与现有的一些答案有些矛盾。
If outputting to a webpage in HTML, particularly text in <textarea>, <pre>or <code>you probably always want to use \nand not PHP_EOL.
如果输出到HTML网页,特别是在文字<textarea>,<pre>或者<code>你可能总是想使用\n,而不是PHP_EOL。
The reason for this is that while code may work perform well on one sever - which happens to be a Unix-like platform - if deployed on a Windows host (such the Windows Azure platform) then it may alter how pages are displayed in some browsers (specifically Internet Explorer - some versions of which will see both the \n and \r).
这样做的原因是,虽然代码可能在一台服务器上运行良好 - 这恰好是一个类 Unix 平台 - 如果部署在 Windows 主机(例如 Windows Azure 平台)上,那么它可能会改变页面在某些浏览器中的显示方式(特别是 Internet Explorer - 某些版本会同时看到 \n 和 \r)。
I'm not sure if this is still an issue since IE6 or not, so it might be fairly moot but seems worth mentioning if it helps people prompt to think about the context. There might be other cases (such as strict XHTML) where suddently outputting \r's on some platforms could cause problems with the output, and I'm sure there are other edge cases like that.
我不确定自 IE6 以来这是否仍然是一个问题,因此它可能相当没有实际意义,但似乎值得一提的是它是否有助于人们提示思考上下文。可能还有其他情况(例如严格的 XHTML),\r在某些平台上突然输出's 可能会导致输出出现问题,我确定还有其他类似的边缘情况。
As noted by someone already, you wouldn't want to use it when returning HTTP headers - as they should always follow the RFC on any platform.
正如有人已经指出的那样,在返回 HTTP 标头时您不希望使用它 - 因为它们应该始终遵循任何平台上的 RFC。
I wouldn't use it for something like delimiters on CSV files (as someone has suggested). The platform the sever is running on shouldn't determine the line endings in generated or consumed files.
我不会将它用于 CSV 文件上的分隔符之类的东西(正如有人建议的那样)。服务器运行的平台不应确定生成或使用的文件中的行尾。
回答by StanE
No, PHP_EOL does not handle endline issues, because the system where you use that constant is not the same system where you send the output to.
不,PHP_EOL 不处理端线问题,因为您使用该常量的系统与您将输出发送到的系统不同。
I would not recommend using PHP_EOL at all. Unix/Linux use \n, MacOS / OS X changed from \r to \n too and on Windows many applications (especially browsers) can display it correctly too. On Windows, it is also easy change existing client-side code to use \n only and still maintain backward-compatibility: Just change the delimiter for line trimming from \r\n to \n and wrap it in a trim() like function.
我根本不推荐使用 PHP_EOL。Unix/Linux 使用 \n,MacOS/OS X 也从 \r 更改为 \n,并且在 Windows 上许多应用程序(尤其是浏览器)也可以正确显示它。在 Windows 上,将现有的客户端代码更改为仅使用 \n 并且仍然保持向后兼容性也很容易:只需将用于行修剪的分隔符从 \r\n 更改为 \n 并将其包装在类似 trim() 的函数中.
回答by ip bastola
I found PHP_EOL very useful for file handling, specially if you are writing multiple lines of content into a file.
我发现 PHP_EOL 对于文件处理非常有用,特别是当您将多行内容写入文件时。
For example, you have a long string that you want to break into the multiple lines while writing into plain file. Using \r\n might not work so simply put PHP_EOL into your script and the result is awesome.
例如,您有一个很长的字符串,您想在写入普通文件时将其分成多行。使用 \r\n 可能不起作用,所以只需将 PHP_EOL 放入您的脚本中,结果就很棒。
Check out this simple example below:
看看下面这个简单的例子:
<?php
$output = 'This is line 1' . PHP_EOL .
'This is line 2' . PHP_EOL .
'This is line 3';
$file = "filename.txt";
if (is_writable($file)) {
// In our example we're opening $file in append mode.
// The file pointer is at the bottom of the file hence
// that's where $output will go when we fwrite() it.
if (!$handle = fopen($file, 'a')) {
echo "Cannot open file ($file)";
exit;
}
// Write $output to our opened file.
if (fwrite($handle, $output) === FALSE) {
echo "Cannot write to file ($file)";
exit;
}
echo "Success, content ($output) wrote to file ($file)";
fclose($handle);
} else {
echo "The file $file is not writable";
}
?>
回答by Edward Z. Yang
The definition of PHP_EOL is that it gives you the newline character of the operating system you're working on.
PHP_EOL 的定义是它为您提供您正在使用的操作系统的换行符。
In practice, you should almost never need this. Consider a few cases:
在实践中,您几乎不需要这个。考虑几种情况:
When you are outputting to the web, there really isn't any convention except that you should be consistent. Since most servers are Unixy, you'll want to use a "\n" anyway.
If you're outputting to a file, PHP_EOL might seem like a good idea. However, you can get a similar effect by having a literal newline inside your file, and this will help you out if you're trying to run some CRLF formatted files on Unix without clobbering existing newlines (as a guy with a dual-boot system, I can say that I prefer the latter behavior)
当您输出到网络时,除了您应该保持一致之外,实际上没有任何约定。由于大多数服务器是 Unixy,因此无论如何您都需要使用“\n”。
如果您要输出到文件,PHP_EOL 似乎是个好主意。但是,您可以通过在文件中添加文字换行符来获得类似的效果,如果您尝试在 Unix 上运行一些 CRLF 格式的文件而不破坏现有的换行符(作为一个拥有双引导系统的人),这将对您有所帮助,我可以说我更喜欢后一种行为)
PHP_EOL is so ridiculously long that it's really not worth using it.
PHP_EOL 太长了,实在不值得使用。
回答by Nica Mlg
DOS/Windows standard "newline" is CRLF (= \r\n) and not LFCR (\n\r). If we put the latter, it's likely to produce some unexpected (well, in fact, kind of expected! :D) behaviors.
DOS/Windows 标准“换行符”是 CRLF (= \r\n) 而不是 LFCR (\n\r)。如果我们使用后者,它可能会产生一些意想不到的(嗯,实际上,有点预期!:D)行为。
Nowadays almost all (well written) programs accept the UNIX standard LF (\n) for newline code, even mail sender daemons (RFC sets CRLF as newlinefor headers and message body).
现在几乎所有(写得很好)程序都接受 UNIX 标准 LF (\n) 换行代码,甚至邮件发送守护程序(RFC 将 CRLF 设置为标题和消息正文的换行符)。
回答by SjH
There is one obvious place where it might be useful: when you are writing code that predominantly uses single quote strings. Its arguable as to whether:
有一个明显的地方可能有用:当您编写主要使用单引号字符串的代码时。关于是否:
echo 'A $variable_literal that I have'.PHP_EOL.'looks better than'.PHP_EOL;
echo 'this other $one'."\n";
The art of it is to be consistent. The problem with mix and matching '' and "" is that when you get long strings, you don't really want to have to go hunting for what type of quote you used.
它的艺术是保持一致。混合和匹配 '' 和 "" 的问题在于,当你得到长字符串时,你真的不想去寻找你使用的引用类型。
As with all things in life, it depends on the context.
与生活中的所有事物一样,这取决于上下文。

