PHP Command Line Progress Bar

KarmaDude Jun 20, 2009

A simple progress bar function for php command line (CLI) scripts:

function progressBar($current, $total, $label="Progress: ", $interval=5) {
    if ($current == 0) {
         echo $label;

         // start the bar
         echo "| 0%";
     } else {
         $pos = ($current / $total * 100);
          if( fmod($pos, $interval) == 0 ) {
              if( $pos >= 10 ) echo "\010";
              if( $pos == 100 ) echo "\010";

               echo "\010\010\010";
               echo "| $pos%";
           }
       }

       // end progress with a new line
       if ($current == $total) echo "\n";
}

Reference code: PHP CLI Progress Indicator