Home » Php » Newline in PHP command line

Newline in PHP command line

Posted by: admin July 12, 2020 Leave a comment


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:


How to&Answers:

You need to use double quotes:

             ^  ^

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


You can concatenate the PHP_EOL constant.

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


Use double quotes echo “next line\n”;


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.


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

echo $array."<br>";


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";

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