Skip to content
Advertisement

Count how many times something was echoed in a range?

I’m just a little stumped, I have a simple range of 1-20 listed out, and displays as follows, showing multiples of 3 within a range, and multiples of 5 within a range. These were displayed with echoes, is there a possible way that I can just count the number of times a specific multiple was displayed/echoed? for instance this is what I have:

JavaScript

but I would like it to show a count like

foo: 6 times listed bar: 4 times listed

Does anyone know of a possible way to use echo substr_count for just words “foo” and “bar” that have been echoed within a range of 1-20?

Let me know if I can clarify. Any guidance would be greatly appreciated!

This is my code:

JavaScript

Advertisement

Answer

Use the output buffering functions to capture echoed output into a variable.

JavaScript

But doing it this way is silly, you can just increment the counters in the loop:

JavaScript

DEMO

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement