I've been doing a reasonable amount of PHP CLI (command line interface) programming recently for getting stuff from log files and converting large numbers of images. Some of these processes can some quite a while to run, sometimes as long as several hours, so it can be useful to know roughly how far through the process it is. This post looks at how to output a counter to the command line from PHP which replaces itself on each iteration.
The important thing with outputting counter information like this is we don't want a big scrolling screen of numbers flying past because it's little messy... this is what would happen if you echo the number out and then follow it with a line break \n
Instead, follow the counter value with a carriage return only \r and each time the value is written out it will replace the line, so you end up with an incrementing value which is always on the same line. This is illustrated in the following example.
This will show e.g. 1/10000, 2/10000, 3/10000 etc always on the same line in your console. At the end we output a final \n newline break so the last line is always displayed.
Running something like this can slow your process down marginally but it's really not going to affect it much if your process is fairly intensive on each loop iteration.
0 comments:
Post a Comment