什么时候使用php_ol是一个好主意?

我有时会在PHP代码示例中看到这种情况。这是否处理DOS/Mac/Unix终端线问题?


当前回答

是的,PHP_EOL表面上用于以跨平台兼容的方式查找换行符,因此它处理DOS/Unix问题。

注意,PHP_EOL表示当前系统的结束字符。例如,当在类unix系统上执行时,它将找不到Windows结束行。

其他回答

当jumi (joomla plugin for PHP)出于某种原因编译你的代码时,它会从你的代码中删除所有的反斜杠。例如$csv_output .= "\n";$csv_output .= "n";

非常讨厌的虫子!

使用PHP_EOL来获得您想要的结果。

我正在使用WebCalendar,发现Mac iCal在导入生成的ics文件时呕吐,因为行尾在xcal.php中硬编码为“\r\n”。我进去,用PHP_EOL替换了所有的事件,现在iCal是快乐的! 我还在Vista和Outlook上进行了测试,即使行尾字符是“\n”,也能导入文件。

PHP_EOL的定义是,它为您提供正在操作的操作系统的换行符。

在实践中,您几乎不需要这个。考虑以下几个案例:

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太长了,真的不值得使用。

您正在编写主要使用单引号字符串的代码。

echo 'A $variable_literal that I have'.PHP_EOL.'looks better than'.PHP_EOL;  
echo 'this other $one'."\n";

如果要输出多行,使用error_log()非常方便。

在我的windows安装中,我发现很多调试语句看起来很奇怪,因为开发人员在拆分字符串时假定unix结尾。