Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active September 24, 2021 00:58
Show Gist options
  • Save vielhuber/7d32629884ff7506a6e0f72d130c6f35 to your computer and use it in GitHub Desktop.
Save vielhuber/7d32629884ff7506a6e0f72d130c6f35 to your computer and use it in GitHub Desktop.
progress bar echo load loading cli #php
<?php
function progress($done, $total, $info = '', $width = 50, $char = '=') {
$perc = round($done * 100 / $total);
$bar = round(($width * $perc) / 100);
echo sprintf(
"%s[%s%s] %s\r",
$info != '' ? $info.' ' : '',
str_repeat($char, $bar).($perc < 100 ? '>' : ''),
$perc == 100 ? $char : str_repeat(' ', $width-$bar),
str_pad($perc, 3, ' ', STR_PAD_LEFT).'%',
);
}
echo 'Searching for TOE (theory of everything)...';
echo PHP_EOL;
$i = 0;
while($i <= 100) {
progress($i, 100, 'Loading...', 75, '#');
usleep($i < 90 ? 10000 : 250000);
$i++;
}
echo PHP_EOL;
echo 'Answer: 42';
echo PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment