Last active
May 29, 2024 14:57
-
-
Save bateller/3613f93542dde6fa222a26ba94d67746 to your computer and use it in GitHub Desktop.
FizzBuzz solution in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} | |
} |
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";
$n = 15;
for ($i = 1; $i <= $n; $i++)
{
if ($i % 15 === 0) {
echo "FizzBuzz
";
}
else if($i % 3 == 0){
echo "Fizz
";
}
else if($i % 5 == 0){
echo "Buzz
";
}
else {
echo $i."
";
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.