PHP CLI 上的新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6818875/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:21:43  来源:igfitidea点击:

New Line on PHP CLI

php

提问by BetaRide

I have a php CLI script and cannot get the output to break on new lines. I do

我有一个 php CLI 脚本,无法让输出在新行上中断。我愿意

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

This gives

这给

this is my text\r\nnext line

Any ideas about how to get the output on different lines?

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

回答by KingCrunch

Use double quotes ".

使用双引号"

echo "next line\n";

Additional you can use the system-dependent constant PHP_EOL

此外,您可以使用系统相关常数 PHP_EOL

echo "this is my text" . PHP_EOL;

回答by Dan Grossman

Escape sequences are only parsed when inside double quotes, not single quotes.

转义序列仅在双引号内时才被解析,而不是单引号内。

http://php.net/manual/en/language.types.string.php

http://php.net/manual/en/language.types.string.php

回答by Daniel A. White

Use double quotes instead. ".

请改用双引号。".

回答by emix

Better notto concatenate anything in PHP, because it may lead to unexpected results, instead use a comma:

最好不要在 PHP 中连接任何东西,因为它可能会导致意想不到的结果,而是使用逗号:

echo 'Text with new line' , PHP_EOL;

This will be faster too by the way: not concatenating and avoiding parsed double quotes.

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