0

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:

1, 2, 3foo, 4, 5bar, 6foo, 7, 8, 9foo, 10bar, 11, 12foo, 13, 14, 15bar, 16, 17, 18foo, 19, 20bar

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:

<?php
foreach (range(1, 20) as $number ) {
echo $number;
echo '&nbsp;';

if ($number % 3 == 0 && $number %5 == 0) {
echo "foobar ";

} elseif ($number % 3 == 0)  {
echo "foo ";

} elseif ($number % 5 == 0) {
echo "bar ";
}

}
echo "<br>";
ob_start();
// code that prints all the numbers
$output = ob_get_flush();
$foo_count = substr_count($output, "foo");
$bar_count = substr_count($output, "bar");

echo "foo: $foo_count times listed<br>";
echo "bar: $bar_count times listed<br>";
?>
Mixmastermiike
  • 449
  • 1
  • 5
  • 16

1 Answers1

3

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

ob_start();

foreach (range(1, 20) as $number ) {
    echo $number;
    echo '&nbsp;';

    if ($number % 3 == 0 && $number %5 == 0) {
        echo "foobar ";

    } elseif ($number % 3 == 0)  {
        echo "foo ";

    } elseif ($number % 5 == 0) {
        echo "bar ";
    }
}

$output = ob_get_flush();
$foo_count = substr_count($output, "foo");
$bar_count = substr_count($output, "bar");

echo "foo: $foo_count times listed<br>";
echo "bar: $bar_count times listed<br>";

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

$foo_count = $bar_count = 0;
foreach (range(1, 20) as $number ) {
    echo $number;
    echo '&nbsp;';

    if ($number % 3 == 0 && $number %5 == 0) {
        echo "foobar ";
        $foo_count++;
        $bar_count++;

    } elseif ($number % 3 == 0)  {
        echo "foo ";
        $foo_count++;

    } elseif ($number % 5 == 0) {
        echo "bar ";
        $bar_count++;
    }
}

DEMO

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hi Barmar - thanks so much for your response, hmm, I've tried to implement that but it doesn't seem to be outputing anything when I try that. I've edited my question and put in the code so you can maybe see what I have... I might be doing something wrong? – Mixmastermiike Oct 20 '20 at 14:39
  • The `foreach` loop needs to replace the comment `// code that prints all the numbers`. You put it before that. – Barmar Oct 20 '20 at 15:30
  • I've updated the answer to show how it fits together. – Barmar Oct 20 '20 at 15:31
  • OHHH I see... duh. Thanks so much, I learned a lot in this exercise. For loops are really hard for me to comprehend. It's all so easy when I fully see what's going on but there's a lot of gaps on the tiny things like needing to know I needed ob_start() stated first (which makes sense). Is there anywhere you'd recommend I read more about this? the documentation for ranges seems a little sparse to me. – Mixmastermiike Oct 20 '20 at 16:09
  • The documentation for `range()` is sparse because there's not much to say about it. All it does is create an array, then you can use all the normal array operations like `foreach`. – Barmar Oct 20 '20 at 16:11
  • gotcha, thanks - yeah it's hard for me to wrap my head around and I don't use this kind of php programming much. – Mixmastermiike Oct 20 '20 at 16:19
  • oh, lastly, is there a way I can count the numbers that integers numbers that would appear? Only counting the ones that do not contain foo / bar? I have been looking at fizz buzz examples but I just don't know how I can only count the number of integers that might appear. I have everything else set. – Mixmastermiike Oct 20 '20 at 17:14
  • You can add `else { $other_count++; }` at the end to count everything else. – Barmar Oct 20 '20 at 17:20
  • Or you can just calculate `$other_count = 20 - $foo_count - $bar_count;` – Barmar Oct 20 '20 at 17:21
  • hmm so, I've tried your second way to increment the counters, but that method actually doesn't seem to produce any count on my end when I test it. the substr_count method in your first solution works, but I'm just not sure how I can count the numbers listed that way? EDIT: ah, nm I see what you were saying. $other_count = 20 - $foo_count - $bar_count; worked with your first suggestion. thank you. – Mixmastermiike Oct 20 '20 at 17:33
  • I don't understand why the second method didn't work. Did you remember to put the code that echoes the counters at the end? I didn't think I needed to say that explicitly. – Barmar Oct 20 '20 at 17:40
  • yup I did - the counts in the second method don't seem to be working for me but that's ok. I really appreciate the help! – Mixmastermiike Oct 20 '20 at 19:12
  • It works for me, I added a link to an online demo. I don't know what you did differently. – Barmar Oct 20 '20 at 19:14