Skip to content

Instantly share code, notes, and snippets.

@stevenpray
Created April 16, 2014 04:01
Show Gist options
  • Save stevenpray/10805251 to your computer and use it in GitHub Desktop.
Save stevenpray/10805251 to your computer and use it in GitHub Desktop.
FizzBuzz PHP
<?php
/**
* FizzBuzz
*
* A program that prints the numbers from 1 to 100.
* Multiples of three print “Fizz” instead of the number, and multiples of five print “Buzz”.
* For numbers which are multiples of both three and five print “FizzBuzz”.
*/
foreach (range(1, 100) as $number) {
$fizzbuzz = "";
if ($number % 3 == 0) {
$fizzbuzz .= "Fizz";
}
if ($number % 5 == 0) {
$fizzbuzz .= "Buzz";
}
printf("%s" . PHP_EOL, empty($fizzbuzz) ? $number : $fizzbuzz);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment