PHP FizzBuzz project

https://www.codecademy.com/courses/learn-php/projects/php-fizzbuzz

Hi all. Still not sure how to do the formatting on here…

I am trying to use a foreach loop using continue and break.

$output = ;

for ($i = 1; $i < 100; $i++) {

if ($i % 3 === 0 && $i % 5 === 0) {

array_push($output, "FizzBuzz\n");

} else if ($i % 3 === 0) {

array_push($output, "Fizz\n");

} else if ($i % 5 === 0) {

array_push($output, "Buzz\n");

} else {

array_push($output, "$i \n");

}

}

foreach ($output as $value) {

if ($value === “Fizz”){

continue;

} else if ($value === “FizzBuzz”) {

echo $value;

break;

}

echo $value . “\n”;

};

Doesnt seem to be breaking or continuing??

Any help would be appreciated. I’m sure it’s something silly but i can’t see it.

Hello! That may be due to the fact that there is no FizzBuzz or Fizz in the $output array. See here:

You’re pushing FizzBuzz\n onto the array. That means every string in the $output array has a newline in it, which is not the same as Fizz or FizzBuzz without a new line.

I hope this helps!

P.S To format your code, press this button:


and insert the code between the back ticks:Screen Shot 2020-05-02 at 14.06.13

1 Like

Gotcha I knew it was something silly. Thanks for replying!

New code:

foreach ($output as $value) {
  if ($value === "Fizz\n"){
    continue;
  } else if ($value === "FizzBuzz\n") {
    echo $value;
    break;
  }
  echo $value . "\n";
};
1 Like

Just curious-why are you appending \n to $value in this line:

Just because that’s the way they formatted it in the hint.

I don’t know actually… :man_shrugging:

echo “$value\n”;

1 Like