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

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


当前回答

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";

它的艺术在于始终如一。混合和匹配"和"的问题是,当你得到很长的字符串时,你真的不想去寻找你使用的引用类型。

就像生活中的所有事情一样,这取决于上下文。

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

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

我刚刚在输出到Windows客户端时遇到了这个问题。当然,PHP_EOL是针对服务器端的,但是php的大部分内容输出都是针对windows客户端的。所以我必须把我的发现放在这里给下一个人看。

A)呼应“我的短信”。PHP_EOL;//不好,因为这只是输出\n,大多数版本的windows记事本显示在一行上,大多数windows会计软件不能导入这种类型的行结束字符。

B)回复“My Text \r\n”;//不好,因为单引号php字符串不解释\r\n

C)回复“My Text \r\n”;//太棒了!在记事本中看起来正确,并在将文件导入到其他windows软件(如windows会计和windows制造软件)时工作。

当你想要一个新的行,并且你想跨平台的时候,你可以使用PHP_EOL。

这可能发生在将文件写入文件系统时(日志、导出、其他)。

如果您希望生成的HTML具有可读性,则可以使用它。所以可以在<br />后面加上PHP_EOL。

如果你在cron上运行php脚本,你需要输出一些东西,并将其格式化后显示在屏幕上,你就可以使用它。

如果你要发送一封需要一些格式的电子邮件,你可以使用它。

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