Yes, another FizzBuzz! I know you guys may be tired of these but I think it's a tradition stepping stone for beginners on this site.
Here are some notes on my thinking:
I tried to keep it as flexible as possible where the logic is deducted from the variables as much as possible. So one could potentially make a user input form on a website and let the user select all the values (numbers and words), and the code just does it.
I tried to avoid magic numbers as much as I know how. Not sure if
0
counts as a magic number.I did not feel the need to add any comments as I feel it is self-explanatory. If you feel otherwise, please let me know.
Here is a link to PhpFiddle for your convenience.
<?php
$counter = 1;
$stopper = 100;
$fizzWord = 'Fizz';
$fizzNumber = 3;
$buzzWord = 'Buzz';
$buzzNumber = 5;
for ($counter = 1; $counter <= $stopper; $counter++) {
if ((($counter %($fizzNumber * $buzzNumber) == 0))) {
echo $fizzWord . $buzzWord;
}
elseif (($counter % $fizzNumber) == 0) {
echo $fizzWord;
}
elseif (($counter % $buzzNumber) == 0) {
echo $buzzWord;
}
else {
echo $counter;
}
echo "<br>\n";
}
?>