Created
June 29, 2015 03:32
-
-
Save veb/792ffa4f7c2ab56c1740 to your computer and use it in GitHub Desktop.
fizzbuzz.php
This file contains 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 | |
/* | |
* 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"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo: http://veb.nz/fizz.php