Skip to content

Instantly share code, notes, and snippets.

@mayconbordin
Created June 2, 2012 23:55
Show Gist options
  • Save mayconbordin/2860547 to your computer and use it in GitHub Desktop.
Save mayconbordin/2860547 to your computer and use it in GitHub Desktop.
PHP CLI progress bar in 5 lines of code
<?php
function progress_bar($done, $total, $info="", $width=50) {
$perc = round(($done * 100) / $total);
$bar = round(($width * $perc) / 100);
return sprintf("%s%%[%s>%s]%s\r", $perc, str_repeat("=", $bar), str_repeat(" ", $width-$bar), $info);
}
@buggsi
Copy link

buggsi commented May 1, 2024

I think it's more consistent to use floor instead of round. And why return, simply echo the output.

function progress_bar($done, $total, $info="", $width=50) {
    $perc = floor(($done * 100) / $total);
    $bar = floor(($width * $perc) / 100);
    echo sprintf("%s%%[%s>%s] %s/%s %s\r", $perc, str_repeat("=", $bar), str_repeat(" ", $width - $bar), $done, $total, $info);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment