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

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**
}

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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

Solution 2

For Windows users :

system('cls');

For Linux users :

system('clear');

Solution 3

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.

Solution 4

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply