New line in PHP command line

I am running a PHP script through the command line and trying to get the output printed on new lines. I tried all the usual suspects ( \n,\r,\l ), but nothing works. I access my Ubuntu server using PuTTY via SSH. Here is my code:

echo($string.'\r');

+4
source share
7 answers

You need to use double quotes:

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

single quotes are no different from any metacharacters other than the backslash itself.

+22
source

You can combine the constant PHP_EOL .

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

Use double quotes echo "next line \ n";

+2
source

For command line script, it is recommended to use PHP_EOL .

Please refer to 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 makes your script executable on the whole platform.

+1
source

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

 echo $array."<br>"; 
-1
source

The accepted answer does not work for me. my attempt 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"; 
Exit

cmd will be:

This is the text before the new line.

text in a new line

-1
source

PHP_EOL is supposedly used to search for a newline character in cross-platform compatibility. By the way, this will also be faster: do not combine and avoid parsable double quotes.

 echo 'This is my 1st line'. PHP_EOL .' 2nd line'; 

Exit

This is my 1st line

2nd row

Use double quotes "" and do not use single quotes ''.

 echo "1st line \n 2nd line "; 

or

 echo "1st line \r\n 2nd line "; 

Exit

1st row

2nd row

-1
source

Source: https://habr.com/ru/post/1414395/


All Articles