Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active June 10, 2023 22:40
Show Gist options
  • Save vielhuber/7614323216039c483afd to your computer and use it in GitHub Desktop.
Save vielhuber/7614323216039c483afd to your computer and use it in GitHub Desktop.
PHP-cli: Show progress on command line through echo #php
<?php
for ($i = 100; $i >= 0; $i--) {
/* every output should not be smaller than the previous one, so otherwise you get output formatting errors */
echo str_pad($i, 4, '0', STR_PAD_LEFT)."\r";
usleep(1000);
}
echo PHP_EOL; /* important on the end */
for ($i = 0; $i <= 100; $i++) {
echo "Loading... {$i}%\r";
usleep(10000);
}
echo PHP_EOL;
for ($i = 100; $i >= 0; $i--) {
//echo $i."\r"; /* this does not work properly */
echo str_pad($i, 99, ' ', STR_PAD_RIGHT)."\r"; /* this works */
usleep(10000);
}
echo PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment