PHP Fizzbuzz Challenge
- by Pez Cuckow
Someone at work as poised the challenge to create a script that prints the FizzBuzz game in as few likes as possible using PHP
The challenge
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
My attempt:
foreach(range(1,100) as $i) {
$val = ($i % 3 == 0 ? "Fizz" : "").($i % 5 == 0 ? "Buzz" : "");
echo (empty($val) ? $i : $val) . '<br />';
}
Someone's Pythons attempt
[ ("Fizz" if not i % 3 else "") + ("Buzz" if not i % 5 else "") + ("Baz" if not i % 7 else "") if _ else "" for i in range(0, 100) ]
Can you see how to make this better/improve it? Or even do it better?
Thanks for your time