How Can Clear Screen in PHP Cli (Like Cls Command)

How can clear screen in php cli (like cls command)

For Windows users :

system('cls');

For Linux users :

system('clear');

Clear PHP CLI output

Try outputting a line of text and terminating it with "\r" instead of "\n".

The "\n" character is a line-feed which goes to the next line, but "\r" is just a return that sends the cursor back to position 0 on the same line.

So you can:

echo "1Done\r";
echo "2Done\r";
echo "3Done\r";

etc.

Make sure to output some spaces before the "\r" to clear the previous contents of the line.

[Edit] Optional: Interested in some history & background? Wikipedia has good articles on "\n" (line feed) and "\r" (carriage return)

PHP CLI, Windows 7 CMD, and CLS/FF need to get along. (Clear Win7 CMD with PHP from CLI)

This is the only, yet very ugly, way of doing this, that I found working so far:

public function clearStdin()
{
for ($i = 0; $i < 50; $i++) echo "\r\n";
}

Clear CMD-shell with php

How about this?

<?php
$i = 1;
echo str_repeat("\n", 300); // Clears buffer history, only executes once
while(1)
{
echo "test_".$i."\r"; // Now uses carriage return instead of new line

sleep(1);
$i++;
}

the str_repeat() function executes outside of the while loop, and instead of ending each echo with a new line, it moves the pointer back to the existing line, and writes over the top of it.

How to update (clear previous value) screen with PHP?

After

echo $a;

You can add

echo exec('clear');

Clear CMD-shell with php

How about this?

<?php
$i = 1;
echo str_repeat("\n", 300); // Clears buffer history, only executes once
while(1)
{
echo "test_".$i."\r"; // Now uses carriage return instead of new line

sleep(1);
$i++;
}

the str_repeat() function executes outside of the while loop, and instead of ending each echo with a new line, it moves the pointer back to the existing line, and writes over the top of it.



Related Topics



Leave a reply



Submit