David Jones
David Jones

Reputation: 10239

Newline in PHP command line

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:

echo($string.'\r');

Upvotes: 8

Views: 27037

Answers (6)

Arbaz Khan
Arbaz Khan

Reputation: 80

The accepted answer not work for me. my try is "\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:

This is some text before newline

text in new line

Upvotes: 0

balduran
balduran

Reputation: 132

For the command line script, it's recommended to use PHP_EOL.

Please refer 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.

Upvotes: 2

JJ23
JJ23

Reputation: 1

PHP there are several many ways. But i think this is what you need

echo $array."<br>";

Upvotes: -1

mpratt
mpratt

Reputation: 1608

You can concatenate the PHP_EOL constant.

echo 'Hi, Im great!' . PHP_EOL;
echo 'And Handsome too' . PHP_EOL;

Upvotes: 21

SKL
SKL

Reputation: 77

Use double quotes echo "next line\n";

Upvotes: 3

Marc B
Marc B

Reputation: 360922

You need to use double quotes:

echo($string."\r");
             ^  ^

single quoted strings do not honor ANY metacharacters, except the backslash itself.

Upvotes: 28

Related Questions