Skip to content

Instantly share code, notes, and snippets.

@veb
Created June 29, 2015 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veb/792ffa4f7c2ab56c1740 to your computer and use it in GitHub Desktop.
Save veb/792ffa4f7c2ab56c1740 to your computer and use it in GitHub Desktop.
fizzbuzz.php
<?php
/*
* Task:
* Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz".
* For numbers which are multiples of both three and five print "FizzBuzz".
*
* Instructions:
* This file (fizzbuzz.php) can be uploaded to a webserver that's running PHP4+ and executed with "php fizzbuzz.php" from the command line.
* Alternatively, this file can be accessed directly (if webserver permissions allow) via your browser.
*
* Author: Mike Mackenzie <mike@veb.co.nz>
*/
for ($i = 1; $i <= 100; $i++)
{
/*
* Using modular arithmetic we can easily compare the given conditions with the integer in the loop to determine the modulus.
* The conditions are 3, 5 and 15. We can use 15 (instead of two big conditional statements) as the aforementioned 3 and 5 are two
* distinct prime factors of 15.
*/
if ($i % 3 == 0)
echo "Fizz";
else if ($i % 5 == 0)
echo "Buzz";
else if ($i % 15 == 0)
echo "FizzBuzz";
else
echo $i;
echo "<br>\n";
}
?>
@veb
Copy link
Author

veb commented Jun 29, 2015

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