何时使用 PHP 常量“PHP_EOL”?
什么时候使用 PHP_EOL
?
我有时会在 PHP 代码示例中看到这一点。 这可以处理 DOS/Mac/Unix 终端问题吗?
When is it a good idea to use PHP_EOL
?
I sometimes see this in code samples of PHP. Does this handle DOS/Mac/Unix endline issues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
我正在使用 WebCalendar,发现 Mac iCal 在导入生成的 ics 文件时遇到问题,因为行尾在 xcal.php 中硬编码为“\r\n”。 我进去并用 PHP_EOL 替换了所有出现的地方,现在 iCal 很高兴!
我还在 Vista 和 Outlook 上进行了测试,即使行尾字符是“\n”,Outlook 也能够导入该文件。
I am using WebCalendar and found that Mac iCal barfs on importing a generated ics file because the end-of-line is hardcoded in xcal.php as "\r\n". I went in and replaced all occurrences with PHP_EOL and now iCal is happy!
I also tested it on Vista and Outlook was able to import the file as well, even though the end of line character is "\n".
可用 当您在服务器的文件系统上读取或写入文本文件时,可以使用此常量。
在大多数情况下,行结尾并不重要,因为大多数软件都能够处理文本文件,无论其来源如何。 您应该与您的代码保持一致。
如果行结束很重要,请显式指定行结束而不是使用常量。 例如:
\r\n
分隔\r\n
作为行分隔符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:
\r\n
\r\n
as row separator不,PHP_EOL 不处理尾行问题,因为您使用该常量的系统与您将输出发送到的系统不同。
我根本不建议使用 PHP_EOL。 Unix/Linux 使用 \n,MacOS / OS X 也从 \r 更改为 \n,并且在 Windows 上许多应用程序(尤其是浏览器)也可以正确显示它。 在 Windows 上,也可以轻松更改现有的客户端代码以仅使用 \n 并仍然保持向后兼容性:只需将行修剪的分隔符从 \r\n 更改为 \n 并将其包装在类似 trim() 的函数中。
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.
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.
我想提出一个答案来解决“当不使用它时”,因为它还没有被涵盖,并且可以想象它被盲目使用,没有人注意到存在问题,直到后来就下线了。 其中一些与现有的一些答案有些矛盾。
如果以 HTML 格式输出到网页,特别是
原因是,虽然代码可能在一台服务器(恰好是类 Unix 平台)上运行良好,但如果部署在 Windows 主机(例如 Windows Azure 平台)上,那么它可能会改变页面在某些浏览器中的显示方式(特别是 Internet Explorer - 某些版本会同时看到 \n 和 \r)。
我不确定自 IE6 以来这是否仍然是一个问题,所以它可能相当没有意义,但似乎值得一提,如果它有助于人们提示思考上下文。 可能还有其他情况(例如严格的 XHTML),在某些平台上突然输出
\r
可能会导致输出问题,而且我确信还有其他类似的边缘情况。正如某人已经指出的那样,您不想在返回 HTTP 标头时使用它 - 因为它们应该始终遵循任何平台上的 RFC。
我不会将它用于 CSV 文件上的分隔符之类的东西(正如有人建议的那样)。 服务器运行的平台不应确定生成或使用的文件中的行结尾。
I'd like to throw in an answer that addresses "When not to 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\n
and notPHP_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).
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.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.
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.
有一个明显的地方它可能很有用:当您编写主要使用单引号字符串的代码时。 关于是否:
它的艺术是一致的,这是有争议的。 混合和匹配 '' 和 "" 的问题在于,当您获得长字符串时,您实际上并不需要去寻找您使用的引用类型。
与生活中的所有事情一样,这取决于具体情况。
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:
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.
我在必须编写的一些命令行脚本中使用 PHP_EOL 常量。 我在本地 Windows 计算机上进行开发,然后在 Linux 服务器上进行测试。 使用常量意味着我不必担心为每个不同的平台使用正确的行结尾。
I use the PHP_EOL constant in some command line scripts I had to write. I develop on my local Windows machine and then test on a Linux server box. Using the constant meant I didn't have to worry about using the correct line ending for each of the different platforms.
我发现 PHP_EOL 对于文件处理非常有用,特别是当您将多行内容写入文件时。
例如,您有一个长字符串,希望在写入普通文件时将其分成多行。 使用 \r\n 可能不起作用,因此只需将 PHP_EOL 放入脚本中,结果就很棒。
看看下面这个简单的例子:
I found PHP_EOL very useful for file handling, specially if you are writing multiple lines of content into a file.
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.
Check out this simple example below:
如果您要输出多行,则使用 error_log() 会很方便。
我发现很多调试语句在我的 Windows 安装上看起来很奇怪,因为开发人员在分解字符串时假设了 UNIX 结尾。
Handy with error_log() if you're outputting multiple lines.
I've found a lot of debug statements look weird on my windows install since the developers have assumed unix endings when breaking up strings.
DOS/Windows 标准“换行符”是 CRLF (= \r\n) 而不是 LFCR (\n\r)。 如果我们采用后者,则可能会产生一些意想不到的(嗯,事实上,有点预期!:D)行为。
如今,几乎所有(编写良好的)程序都接受 UNIX 标准 LF (\n) 作为换行代码,甚至邮件发送器守护程序(RFC 将 CRLF 设置为标头和消息正文的 换行)。
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.
Nowadays almost all (well written) programs accept the UNIX standard LF (\n) for newline code, even mail sender daemons (RFC sets CRLF as newline for headers and message body).
PHP_EOL 的定义是它为您提供正在使用的操作系统的换行符。
实际上,您几乎不需要这个。 考虑几种情况:
当您输出到网络时,除了应该保持一致之外,实际上没有任何约定。 由于大多数服务器都是 Unixy,因此您无论如何都需要使用“\n”。
如果您要输出到文件,PHP_EOL 似乎是个好主意。 然而,你可以通过在你的文件中包含一个文字换行符来获得类似的效果,如果你试图在 Unix 上运行一些 CRLF 格式的文件而不破坏现有的换行符,这将帮助你(作为一个拥有双引导系统的人) ,我可以说我更喜欢后一种行为)
PHP_EOL 太长了,以至于真的不值得使用它。
The definition of PHP_EOL is that it gives you the newline character of the operating system you're working on.
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)
PHP_EOL is so ridiculously long that it's really not worth using it.
我有一个网站,在用户(可以使用任何操作系统)执行操作后,日志记录脚本将新的文本行写入文本文件。
在这种情况下,使用 PHP_EOL 似乎不是最佳选择。 如果用户使用 Mac OS 并写入文本文件,它将输入 \n。 在 Windows 计算机上打开文本文件时,它不显示换行符。 因此,我使用“\r\n”代替,它在任何操作系统上打开文件时都有效。
I have a site where a logging-script writes a new line of text to a textfile after an action from the user, who can be using any OS.
Using PHP_EOL don't seem to be optimal in this case. If the user is on Mac OS and writes to the textfile it will put \n. When opening the textfile on a windows computer it doesn't show a line break. For this reason i use "\r\n" instead which works when opening the file on any OS.
我刚刚在输出到 Windows 客户端时遇到了这个问题。 当然,PHP_EOL 是针对服务器端的,但是 php 输出的大多数内容是针对 Windows 客户端的。 所以我必须把我的发现放在这里供下一个人使用。
A) 回显“我的文本”。 PHP_EOL; // 不好,因为这仅输出 \n 并且大多数版本的 Windows 记事本将其显示在一行上,并且大多数 Windows 会计软件无法导入这种类型的行尾字符。
B) echo '我的文本\r\n'; //不好,因为单引号 php 字符串不能解释 \r\n
C) echo "My Text \r\n"; // 是的,它有效! 在记事本中看起来正确,并且在将文件导入到其他 Windows 软件(例如 Windows 会计和 Windows 制造软件)时有效。
I just experienced this issue when outputting to a Windows client. Sure, PHP_EOL is for server side, but most content output from php is for windows clients. So I have to place my findings here for the next person.
A) echo 'My Text' . PHP_EOL; // Bad because this just outputs \n and most versions of windows notepad display this on a single line, and most windows accounting software can't import this type of end of line character.
B) echo 'My Text \r\n'; //Bad because single quoted php strings do not interpret \r\n
C) echo "My Text \r\n"; // Yay it works! Looks correct in notepad, and works when importing the file to other windows software such as windows accounting and windows manufacturing software.
您正在编写主要使用单引号字符串的代码。
You are writing code that predominantly uses single quote strings.
我更喜欢使用\n\r。 另外,我使用的是 Windows 系统,根据我的经验,\n 工作得很好。
由于 PHP_EOL 不适用于正则表达式,而这些是处理文本的最有用的方法,所以我真的从未使用过或不需要它。
I prefer to use \n\r. Also I am on a windows system and \n works just fine in my experience.
Since PHP_EOL does not work with regular expressions, and these are the most useful way of dealing with text, then I really never used it or needed to.
当 jumi(PHP 的 joomla 插件)由于某种原因编译你的代码时,它会从你的代码中删除所有反斜杠。 这样
$csv_output .= "\n";
就变成了$csv_output .= "n";
非常烦人的错误!
使用 PHP_EOL 来获得您想要的结果。
When jumi (joomla plugin for PHP) compiles your code for some reason it removes all backslashes from your code. Such that something like
$csv_output .= "\n";
becomes$csv_output .= "n";
Very annoying bug!
Use PHP_EOL instead to get the result you were after.
在某些系统上使用这个常量可能很有用,因为例如,如果您正在发送电子邮件,您可以使用 PHP_EOL 让跨系统脚本在更多系统上工作...但即使有时它很有用,您也可以找到这个常量未定义,使用最新 php 引擎的现代托管没有这个问题,但我认为编写一些代码来保存这种情况是一件好事:
因此您可以毫无问题地使用 PHP_EOL...显然 PHP_EOL 应该在脚本上使用应该同时在多个系统上工作,否则您可以使用 \n 或 \r 或 \r\n...
注意:PHP_EOL 可以
希望这个答案有所帮助。
On some system may be useful to use this constant because if, for example, you are sending an email, you can use PHP_EOL to have a cross-system script working on more systems... but even if it's useful sometime you can find this constant undefined, modern hosting with latest php engine do not have this problem but I think that a good thing is write a bit code that saves this situation:
So you can use PHP_EOL without problems... obvious that PHP_EOL should be used on script that should work on more systems at once otherwise you can use \n or \r or \r\n...
Note: PHP_EOL can be
Hope this answer help.
是的,
PHP_EOL
表面上用于以跨平台兼容的方式查找换行符,因此它可以处理 DOS/Unix 问题。请注意,PHP_EOL 表示当前的结束行字符系统。 例如,在类 UNIX 系统上执行时,它不会找到 Windows 结束行。
Yes,
PHP_EOL
is ostensibly used to find the newline character in a cross-platform-compatible way, so it handles DOS/Unix issues.Note that PHP_EOL represents the endline character for the current system. For instance, it will not find a Windows endline when executed on a unix-like system.
从 PHP 7.1.1 版和 5.6.30 版的
main/php.h
来看:如您所见,
PHP_EOL
可以是"\r\n"(在 Windows 服务器上)或
"\n"
(在其他任何服务器上)。 在 PHP 5.4.0RC8 之前的版本中,PHP_EOL
有第三个可能的值:"\r"
(在 MacOSX 服务器上)。 这是错误的,已于 2012 年 3 月 1 日通过 bug 61193 进行修复。正如其他人已经告诉您的那样,您可以在任何类型的输出中使用 PHP_EOL(其中任何这些值都是有效的 - 例如:HTML、XML、日志...),其中您想要统一的换行符。 请记住,决定该值的是服务器,而不是客户端。 您的 Windows 访问者将从您的 Unix 服务器获取价值,这有时对他们来说不方便。
我只是想显示 PHP 源支持的
PHP_EOL
的可能值,因为它尚未在此处显示......From
main/php.h
of PHP version 7.1.1 and version 5.6.30:As you can see
PHP_EOL
can be"\r\n"
(on Windows servers) or"\n"
(on anything else). On PHP versions prior 5.4.0RC8, there were a third value possible forPHP_EOL
:"\r"
(on MacOSX servers). It was wrong and has been fixed on 2012-03-01 with bug 61193.As others already told you, you can use
PHP_EOL
in any kind of output (where any of 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.I just wanted to show the possibles values of
PHP_EOL
backed by the PHP sources since it hasn't been shown here yet...当您想要换行并且想要跨平台时,可以使用 PHP_EOL。
这可能是当您将文件写入文件系统(日志、导出等)时。
如果您希望生成的 HTML 可读,则可以使用它。 因此,您可以在
后面加上PHP_EOL
。如果您从 cron 中将 php 作为脚本运行,并且需要输出一些内容并将其格式化为屏幕格式,则可以使用它。
如果您正在创建一封需要某些格式的电子邮件来发送,则可以使用它。
You use
PHP_EOL
when you want a new line, and you want to be cross-platform.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 aPHP_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.
You might use it if you are building up an email to send that needed some formatting.