Skip to content

Instantly share code, notes, and snippets.

@mertvetsky
Last active March 21, 2016 15:34
Show Gist options
  • Save mertvetsky/7d04d7e0195b56e3dc61 to your computer and use it in GitHub Desktop.
Save mertvetsky/7d04d7e0195b56e3dc61 to your computer and use it in GitHub Desktop.
php-cli progress bar
<?php
function progressBar($cur = 0, $max = 0, $full = '#', $empty = ' ')
{
$p = (int)($cur / $max * 100);
return $max < 100 || $cur % (int)($max / 100) == 0 || $p == 100 ?
sprintf("\r[%s%s] %d%% %d/%d", str_repeat($full, $p), str_repeat($empty, 100 - $p), $p, $cur, $max) : '';
}
// demo
$max = 54321;
foreach (range(0, $max) as $i) {
echo progressBar($i, $max);
usleep(rand(10, 200));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment