PHP 命令行中的换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10756714/
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
Newline in PHP command line
提问by David Jones
I'm running a PHP script via the command line and trying to get output printed on new lines. I've tried all of the usual suspects (\n,\r,\l) but nothing is working. I'm accessing my Ubuntu server using PuTTY over SSH. Here's my code:
我正在通过命令行运行 PHP 脚本并尝试在新行上打印输出。我已经尝试了所有常见的嫌疑人 ( \n,\r,\l),但没有任何效果。我正在通过 SSH 使用 PuTTY 访问我的 Ubuntu 服务器。这是我的代码:
echo($string.'\r');
echo($string.'\r');
回答by Marc B
You need to use double quotes:
您需要使用双引号:
echo($string."\r");
^ ^
single quoted strings do not honor ANY metacharacters, except the backslash itself.
单引号字符串不支持任何元字符,除了反斜杠本身。
回答by mpratt
You can concatenate the PHP_EOLconstant.
您可以连接PHP_EOL常量。
echo 'Hi, Im great!' . PHP_EOL;
echo 'And Handsome too' . PHP_EOL;
回答by SKL
Use double quotes echo "next line\n";
使用双引号echo "next line\n";
回答by balduran
For the command line script, it's recommended to use PHP_EOL.
对于命令行脚本,建议使用PHP_EOL.
Please refer http://php.net/manual/en/reserved.constants.php
请参考http://php.net/manual/en/reserved.constants.php
PHP_EOL (string)
The correct 'End Of Line' symbol for this platform. Available since PHP 5.0.2
It make your script executable across platform.
它使您的脚本可以跨平台执行。
回答by Arbaz Khan
The accepted answer not work for me.
my try is "\r\n"
接受的答案对我不起作用。我的尝试是"\r\n"
echo 'This is some text before newline';
echo "\r\n"."text in new line";
//OR
echo 'This is some text before newline'."\r\n"."text in new line";
cmd output will be:
cmd 输出将是:
This is some text before newline
这是换行前的一些文字
text in new line
新行中的文本
回答by JJ23
PHP there are several many ways. But i think this is what you need
PHP有好几种方法。但我认为这就是你所需要的
echo $array."<br>";

