Last active
March 29, 2021 07:22
-
-
Save amculin/fdca509c7ad589a12545ed11e389aed6 to your computer and use it in GitHub Desktop.
FizzBuzz Challenge in PHP using Console Command
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 | |
class FizzBuzz | |
{ | |
public $start; | |
public $last; | |
public function __construct($start=0, $last=100) | |
{ | |
$this->start = $start; | |
$this->last = $last; | |
} | |
public function run() | |
{ | |
for ($i = $this->start; $i <= $this->last; $i++) { | |
$isFizz = $i % 3 == 0; | |
$isBuzz = $i % 5 == 0; | |
if ($isFizz) | |
echo "Fizz"; | |
if ($isBuzz) | |
echo "Buzz"; | |
if (! $isFizz && ! $isBuzz) | |
echo $i; | |
echo PHP_EOL; | |
} | |
} | |
} | |
$fizzBuzz = new FizzBuzz($argv[1], $argv[2]); | |
$fizzBuzz->run(); | |
//Example: php FizzBuzz.php 1 100 | |
?> |
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 | |
function fizz_buzz($start, $last) | |
{ | |
for ($i = $start; $i <= $last; $i++) { | |
$isFizz = $i % 3 == 0; | |
$isBuzz = $i % 5 == 0; | |
if ($isFizz) | |
echo "Fizz"; | |
if ($isBuzz) | |
echo "Buzz"; | |
if (! $isFizz && ! $isBuzz) | |
echo $i; | |
echo PHP_EOL; | |
} | |
} | |
fizz_buzz($argv[1], $argv[2]); | |
//Example: php simpleFizzBuzz.php 1 100 | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment