photo credit: Base System via photopin (license)

PHP CLI Progress Logger

Tags: ,

As some of you might already know, I make great use of the Command Line Interface (CLI) of PHP for tasks or tools that need to be run incidentally or periodically.
For tasks that have iterations in them (like while  or foreach -loops) I like to have a visual feedback in my terminal of the progress. For example:

  • What’s the total number of iterations?
  • What’s the current iteration?
  • How many percent is already done?
  • How much longer does it take?
  • How much memory is PHP currently consuming?

I created a very simple function to provide this feedback. It’s output looks a bit like this:

Screen Shot 2015-02-24 at 12.01.23
Today I’m going to share this function with you.

The function is called: echoProgress() , and it takes 2 parameters:

  1. The number of the current iteration.
  2. The number of the total amount of iterations that are being executed.

Once called within your while() , foreach()  or whatever iteration structure you’re using, it echo’s the current status to the terminal. Please note that it ends it’s line with \r  and not with \n . This little trick makes sure you’re seeing a nice little progress bar instead of thousands of lines being spit out.
Use it as you please. I know I do!

Visitors give this article an average rating of 4.3 out of 5.

How would you rate this article?

Leave a Reply