-
-
Save bateller/3613f93542dde6fa222a26ba94d67746 to your computer and use it in GitHub Desktop.
<?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 You're correct. I've updated the gist.
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;
}
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.
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 :)
for($i=1;$i<=20;$i++) {
$out = ($i%3?'':'Fizz').($i%5?'':'Buzz');
echo ($out?$out:$i)."\n";
}
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 . "
";
}
}
<?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";
}
@SephGER this would run a calculation 200 times (twice for every loop) while OPs would run a modulos calculation 47 times.
What about:
<?php
for($i = 1; $i <= 100; $i++)
echo ($i % 3 === 0 ? 'Fizz' : '') .
($i % 5 === 0 ? 'Buzz' : '')
?: $i, PHP_EOL;
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
"); } } ?>
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))));
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";
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";
Why are u doing 2 more operations (% and ===) + 1 logical operator in expression:
when u can just use
?