Loading / Processing in PHP cli

So that it would display from "Loading 1%" to "Loading 100%". But this will result in all appearing one at a time without disappearing after the new line appears. So I want to know how to make the new line appear and the old disappear and this starts after the page loads, so the user will be able to watch a loader actually loading from 1% to 100%.
In order to give the impression that the line is being overwritten, you can insert a CR (Carriage Return, "\r") in front of your output. I wrote it down on an example:
<?php
$x=1;
while($x<=100) {
   echo "\rLoading: $x %";
   usleep(10000);
   $x++;
}
?>
Running this on a terminal I get a cool loading status: $ php code.php
Hope that's what you've been looking for.

Popular Posts