1

I have a PHP file that currently runs via my console on my Mac. It's a private piece of software that interacts with an API via a loop that runs every x seconds.

At the minute Im am echoing out all information during each loop but the problem is within console it just repeats the content and appends it on to the previous. Is there a way of clearing all previous echoed results after each loop?

I thought I could use \r to replace the line but that doesn't seem to be working. I have also tried a few other examples after searching on here without any luck.

while(1){

echo "Some info goes here";

sleep(5);

}

For example if I try the following in a while loop

while(1){
    echo "==============\r\n";
    echo "==============\r\n";

sleep(5);
}

I end up with enter image description here after a hand full of loops

Thanks

ORStudios
  • 3,157
  • 9
  • 41
  • 69

3 Answers3

2

Two ways that I can think of off the top of my head:

  • Use the ANSI escape sequence to clear the screen:

    print chr(27) . "[2J" . chr(27) . "[;H";

  • Run the command that clears the screen for your platform:

    system("cls"); for windows

    system("clear"); for unix-like systems

Ben Harris
  • 547
  • 3
  • 10
1
echo "\033[2J\033[;H";

Should clear your terminal.

Scoots
  • 3,048
  • 2
  • 21
  • 33
1

If you just want to overwrite the same line again, this is working for me:

<?php
while(1) {
  echo "test\r";
  sleep(1);
}
?>
micdel
  • 11
  • 2