PHP CLI上的新行


106

我有一个PHP CLI脚本,无法在新行中中断输出。我做

echo 'this is my text\r\n';
echo 'next line';

这给

this is my text\r\nnext line

关于如何在不同的行上获得输出的任何想法?


4
使用双引号:echo "\n";
Kerrek SB 2011年

Answers:


242

使用双引号"

echo "next line\n";

另外,您可以使用与系统有关的常量 PHP_EOL

echo "this is my text" . PHP_EOL;

34
使用PHP_EOL应该是添加新行的首选方法。
AutomaticPixel

3
@AutomaticPixel对于平台兼容性,是的,对于跨平台兼容性,您应该使用\n
KingCrunch

2
这很有帮助,而且是真正快速的解决方案:)分享tnx
Aditya P Bhatt

3
@KingCrunch“平台间兼容性”是什么意思?
edigu 2014年

1
@foozy例如,您在linux上生成了一个东西,但想在Mac系统或Win系统上阅读它。如果您不知道目标系统,或者它可能会更改(->某人转发带有生成文件作为附件的邮件),请使用\n
KingCrunch



3

最好不要连接PHP中的任何内容,因为它可能导致意外的结果,而请使用逗号:

echo 'Text with new line' , PHP_EOL;

顺便说一句,这也将更快:不连接并避免解析的双引号。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.