FizzBuzz in PHP

Apr 12 2007

I was reading Why Can’t Programmers… Program today and while I find questions like these annoying at interviews because PHP isn’t really used for that sort of thing, I set myself and my colleagues the task of writing out answers.

The question:

Print out the numbers 1 to 100. Where the number is a multiple of 3, print ‘Fizz’, otherwise if it is a multiple of 5 print ‘Buzz’. If the number is a multiple of 3 and 5, print ‘FizzBuzz’.

Here are our answers, and mind you, they’re all in PHP.

Lim is our budding project coordinator turned programmer. He’s just started getting into PHP, and here’s what Lim came up with:

for ($count=1; $count<=100; $count++)
{
    if ($count%3 == 0 && $count%5 ==0){
        print "FizzBuzz<br />";
    }
    elseif ($count%3 == 0){
        print "Fizz<br />";
    }
    elseif ($count%5 == 0){
        print "Buzz<br />";
    }
    else {
        print $count."<br />";
    }
}

An almost perfect textbook style answer.

Here’s what Foong came up with:

for($i=1;$i<=100;$i++)
{
    $result=$i/3;
    $result2=$i/5;

    if(!preg_match("[\.]" , $result)&&!preg_match("[\.]" , $result2))
    {
        echo "FizzBuzz<br />";
    }
    elseif(!preg_match("[\.]" , $result))
    {
        echo "Fizz<br />";
    }
    elseif(!preg_match("[\.]" , $result2))
    {
        echo "Buzz<br />";
    }
    else
    {
        echo $i."<br />";
    }
}

Certainly an example of … unique thinking. Foong’s had more than a year of experience programming in PHP, for the record.

Here’s what I came up with:

foreach(range(1,100) as $i)
    echo $i % 3 == 0 ? ( $i % 5 == 0 ? "FizzBuzz\r\n" : "Fizz\r\n" ) : ( $i % 5 == 0 ? "Buzz\r\n" : $i."\r\n" );

Update: I’ve made a change to the code, Ken Brush a.k.a. shiruken noticed I’d missed something out :(

I love the conditional operator @_@

Also, no, I would never use code like this in production, or even casual coding. I just wanted to beat my colleagues on line-count :P

And now, the ‘proper’ textbook solution for all the Googlers:

for ($i = 1; $i <= 100; $i++)
{
    if($i % 3 == 0 && $i % 5 ==0){
        print "FizzBuzz<br />";
    }
    else if($i % 3 == 0){
        print "Fizz<br />";
    }
    else if($i % 5 == 0){
        print "Buzz<br />";
    }
    else {
        print $i."<br />";
    }
}

9 responses so far

  1. Well, the second approach is overkill really, and uses extra processing power. The first and last examples are the most efficient really when it comes to readability and processing.

  2. I like how the conditional doesn’t print Buzz, even though that’s a requirement.

    wouldn’t this work better:

    foreach (range(1,100) as $number) { $msg=”; if ( $number%3 == 0) { $msg .= “Fizz”; } if ($number%5 == 0) { $msg .= “Buzz”; } if (empty($msg)) { $msg = $number; } print “$msg\n”; }

    Of course, being the conditional lover I am, I would probably write the last if (the empty check) to this:

    foreach (range(1,100) as $number) { $msg=”; if ( $number%3 == 0) { $msg .= “Fizz”; } if ($number%5 == 0) { $msg .= “Buzz”; } printf (“%s\n”, (empty($msg) ? $number : $msg)); }

  3. Heh, noticed you changed the conditional in the main article now.

  4. I really liked this but,

    for ($num=1; $num<=100; $num++) { if ($num%3 == 0) { echo “Fizz”; $keepNum = true; } if ($num%5 == 0) { echo “Buzz”; $keepNum = true; } if (!$keepNum) { echo $num; } $keepNum = false;

        echo "<br />";
    

    }

    I really like yours Ken, I cleaned it up a bit ;-)

    foreach (range(1,100) as $num) { $msg =”; if ($num%3 == 0) $msg = “Fizz”; if ($num%5 == 0) $msg .= “Buzz”; if (!$msg) $msg = $num; print “$msg\n”; }

  5. How about 75 bytes? Geeky enough? :)

    for($i=1;$i<101;$i++)echo $i%15?$i%5?$i%3?$i:’Fizz’:'Buzz’:'FizzBuzz’,”\n”;

  6. Wow Jyrki that certainly is a very umm compact answer :P

  7. I found this post randomly googling some PHP issue and thought I would put my spin on it. I consider myself a below entry level programmer, it’s a hobby that I hope to someday turn into a job. Anyway, here’s my convoluted solution.

    $c = “Fizz”; $d = “Buzz”; $e = “n”;

    for($i=1;$i<=100;$i++){ $a = $i%3; $b = $i%5; if(!$a){ if(!$b){ echo $c.$d.$e; }else{ echo $c.$e; } }elseif(!$b){ echo $d.$e; }else{ echo $i.$e; } }

  8. pretty enough?

    <?php foreach (range(1, 100) as $i) { $mod=array($i,”Bizz”,”Buzz”,”BizzBuzz”); echo $mod[(!($i%5))+2*($i%7==0)].’ ‘; }

  9. I think the FizzBuzz tests whether someone remembers that there is a modulus operator (%). All of the poor solutions I have seen are where people didn’t know/forgot that they could use %.

    Here’s another solution just for fun:

    for ($i=1,$f=’Fizz’,$b=’Buzz’;$i<=100;$i++) echo $i%3+$i%5<1 ? $f.$b : ($i%3<1 ? $f : ($i%5<1 ? $b : $i)), ”;

Leave a Reply