Created
July 1, 2019 16:15
-
-
Save hissy/abc443299e8a564048ba1fbde8337acb to your computer and use it in GitHub Desktop.
FizzBuzz 作例:functionを使う
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 | |
/** | |
* @param int $start | |
* @param int $end | |
* @return string | |
*/ | |
function fizzbuzz(int $start, int $end) { | |
$output = ''; | |
$numbers = range($start, $end); | |
foreach ($numbers as $number) { | |
if ($number % 3 === 0) { | |
$output .= 'Fizz'; | |
} elseif ($number % 5 === 0) { | |
$output .= 'Buzz'; | |
} elseif ($number % 15 === 0) { | |
$output .= 'FizzBuzz'; | |
} else { | |
$output .= $number; | |
} | |
$output .= PHP_EOL; | |
} | |
return $output; | |
} | |
echo fizzbuzz(1, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment