PHP Fizzbuzz Challenge

Posted by Pez Cuckow on Stack Overflow See other posts from Stack Overflow or by Pez Cuckow
Published on 2012-10-05T15:19:07Z Indexed on 2012/10/05 15:37 UTC
Read the original article Hit count: 325

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about php

Related posts about fizzbuzz