Home » Php » Newline in PHP command line

Newline in PHP command line

Posted by: admin July 12, 2020 Leave a comment

Questions:

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');

How to&Answers:

You need to use double quotes:

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

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

Answer:

You can concatenate the PHP_EOL constant.

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

Answer:

Use double quotes echo “next line\n”;

Answer:

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.

Answer:

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

echo $array."<br>";

Answer:

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