Skip to content

Instantly share code, notes, and snippets.

@hissy
Created July 1, 2019 16:15
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 hissy/abc443299e8a564048ba1fbde8337acb to your computer and use it in GitHub Desktop.
Save hissy/abc443299e8a564048ba1fbde8337acb to your computer and use it in GitHub Desktop.
FizzBuzz 作例:functionを使う
<?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