Skip to content

Instantly share code, notes, and snippets.

@bateller
Last active February 28, 2024 11:00
Show Gist options
  • Save bateller/3613f93542dde6fa222a26ba94d67746 to your computer and use it in GitHub Desktop.
Save bateller/3613f93542dde6fa222a26ba94d67746 to your computer and use it in GitHub Desktop.
FizzBuzz solution in PHP
<?php
if (php_sapi_name() === 'cli') $lb = "\n";
else $lb = "<br />";
for ($i = 1; $i <= 100; $i++)
{
if ($i % 15 === 0) {
echo "FizzBuzz $lb";
}
else if($i % 3 == 0){
echo "Fizz $lb";
}
else if($i % 5 == 0){
echo "Buzz $lb";
}
else {
echo $i." $lb";
}
}
@arthurkushman
Copy link

arthurkushman commented May 4, 2019

Why are u doing 2 more operations (% and ===) + 1 logical operator in expression:

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

when u can just use

if ($i % 15 === 0)

?

@bateller
Copy link
Author

@arthurkushman You're correct. I've updated the gist.

@cherifGsoul
Copy link

Here is my version:

function fizzbuzz(int $num): string
{
        $isFizz = $num % 3 == 0;
        $isBuzz = $num % 5 == 0;
        $fizzBuzz = ($isFizz ? 'fizz' : '') . ($isBuzz ? 'buzz' : '');
        return  !empty($fizzBuzz) ? $fizzBuzz : (string)$num;
}

@bateller
Copy link
Author

Nice @cherifGsoul !

Yeah I totally agree the code could be minified using PHP shorthand, etc.. but originally I wanted to show to a newbie how to do FizzBizz where they can follow along easily in the logic.

Nicely done though. Nice to show there are different ways to approach the same problem.

@ADCPD
Copy link

ADCPD commented Nov 3, 2020

There is my version :

function fizzBuzz($number,array $array)
{
    if (array_key_exists($number, $array)) {
        $number =  $array[$number];
        return strval($number);
    } else {
        foreach($array as $key => $value) {
            $div = $number/$key;
            if(array_key_exists(intval($div),$array)) {
                if ($key < $div) {
                    return  $array[$key] . "" .  $array[$div];
                } else {
                    return  $array[$div] . "" .   $array[$key];
                }
            }
            return $array[$key];
        }
    }
}

Clic to https://3v4l.org/eHjrv to run the code :) Is correctly working :)

@anyeor
Copy link

anyeor commented Jun 11, 2021

for($i=1;$i<=20;$i++) {
$out = ($i%3?'':'Fizz').($i%5?'':'Buzz');
echo ($out?$out:$i)."\n";
}

@rakanugro
Copy link

rakanugro commented Jun 16, 2021

for ($i = 1 ; $i <= 100; $i++)
{
if($i % 3 == 0 && $i % 5 == 0 )
{

  echo 'fizzbuzz <br />';

}

elseif($i %3 ==0)
{
echo 'fizz
';

}

elseif($i % 5 == 0 )
{
echo "Buzz
";
}

else{
echo $i . "
";
}

}

@SephGER
Copy link

SephGER commented Jul 12, 2021

<?php
for($i=1;$i<=100;$i++){
    switch([$i%3==0,$i%5==0]){
        case [1,0] : echo "Fizz"; break;
        case [0,1] : echo "Buzz"; break;
        case [1,1] : echo "FizzBuzz"; break;
        default   : echo $i; break;
    }
    echo "\n";
}

@ryanmortier
Copy link

ryanmortier commented Dec 3, 2021

@SephGER this would run a calculation 200 times (twice for every loop) while OPs would run a modulos calculation 47 times.

@maduranma
Copy link

maduranma commented Mar 11, 2022

What about:

<?php
for($i = 1; $i <= 100; $i++)
    echo  ($i % 3 === 0 ? 'Fizz' : '') .
          ($i % 5 === 0 ? 'Buzz' : '')
          ?: $i, PHP_EOL;

@tafadzwamunyuki
Copy link

Here is a very easy to understand way to solve FizzBuzz in PHP:

"); }else // Print Buzz if($x % 5 == 0){ echo("Buzz
"); // Print number value }elseif ($x != "Fizz" && $x != "Buzz") { echo("$x
"); } } ?>

@pfrenssen
Copy link

Had a go with an arrow function, nice and short :)

print(implode("\n",array_map(fn($i)=>(!($i%3)?'Fizz':'').(!($i%5)?'Buzz':'')?:$i,range(1,100))));

@maduranma
Copy link

This is the shortest I could get without throwing errors. You could replace \n by literally and enter, but it'd be no longer a one liner.

for(;@$i++<100;)echo@(['Fizz'][$i%3].['Buzz'][$i%5]?:$i),"\n";

@maiorano84
Copy link

maiorano84 commented Nov 10, 2023

Here's the shortest I could get without throwing errors OR error silencing:

for($i=0;$i++<100;)echo($i%3?'':'Fizz').($i%5?'':'Buzz')?:$i,"\n";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment