1

I'd like to do a multi line carriage return with php for the command line. Is this possible?

I know already how to achieve it with one line print("\r") but it like to know how to do this on multiple lines.

The printed data should look like this:

$output = "
 Total time passed: 34, \n
 Total tests: 14/523 
";
print($output . "\r");

It works for a single line so that there is not a new line added every time it is printed in the loop. When i use it with a PHP_EOL, or \n i'm getting new lines all the time. I just want two lines to show up while updating it.

HerrWalter
  • 622
  • 1
  • 5
  • 13
  • You basically want it to overwrite the old output with the new output. So the end result is the lines from your output look as though they are being updated rather than repeated. – Nigel Ren Jun 08 '18 at 14:44

3 Answers3

3

You can do this using ANSI escape char [F

As example:

for ($i = 1; $i < 5; $i++) {
    $prevLineChar = "\e[F";
    sleep(1);
    echo "$i\nof 5\n" . $prevLineChar . $prevLineChar;
}
Artem Ilchenko
  • 1,050
  • 9
  • 20
0

You are looking for PHP_EOL, one for each carriage return. See here: https://stackoverflow.com/a/128564/2429318 for a nice friendly description.

It is one of the Core Predefined Constants in PHP

PHP_EOL (string) The correct 'End Of Line' symbol for this platform. Available since PHP 5.0.2

ajmedway
  • 1,492
  • 14
  • 28
0

You could write something like:

print("\n\n\n\n\n");

but what if you want to print a dynamic number of new lines :)

try this function:

function new_lines($n) {
    for($i = 0; $i < $n; $i++) { echo PHP_EOL; }
}

call it like so :

//print 100 new lines
new_lines(100); 
xanadev
  • 751
  • 9
  • 26