31

When PHP script run from command line (windows) , how can clear the console screen from script .

for example :

while(true){
    // sleep for 10 seconds , then clear the console
    sleep(10);

    // below command execute to clear the console windows
    **COMMAND**
}
Root
  • 2,269
  • 5
  • 29
  • 58

4 Answers4

58

If you did not have any luck with the solutions above, consider the following

echo chr(27).chr(91).'H'.chr(27).chr(91).'J';   //^[H^[J  

Hope it will help.

Source : http://pank.org/blog/2011/02/php-clear-terminal-screen.html

M. Chris
  • 907
  • 1
  • 7
  • 5
19

For Windows users :

system('cls');

For Linux users :

system('clear');
checksum
  • 6,415
  • 4
  • 36
  • 34
OlivierLarue
  • 2,234
  • 27
  • 28
5

Found a solution, that works in both cmd and GitBash. However, this is the ugliest implementation of clearing console-screen I can think of. Pity, that there isn't any working alternative.

The "magic" is to... poke console with fifty new-lines, like that:

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

This is a modified (fixed?) version of this non-working (for me) post from 2006.

trejder
  • 17,148
  • 27
  • 124
  • 216
0

You can do this by using:

ncurses_clear();

Source: http://www.php.net/manual/en/function.ncurses-clear.php

Edit: As trejder says this solution is only for supported platforms, it seems windows is not one of them.

Lost F.
  • 410
  • 3
  • 15
  • 9
    OP asks for a **Windows**-based soluton! `ncurses` library [is **NOT** available](http://php.net/manual/en/intro.ncurses.php) for Windows platform in PHP. – trejder May 07 '15 at 12:46