Skip to content

Instantly share code, notes, and snippets.

@falseresync
Created November 14, 2023 18:09
Show Gist options
  • Save falseresync/40a8330a98f87d2a1b6b7efc23c87284 to your computer and use it in GitHub Desktop.
Save falseresync/40a8330a98f87d2a1b6b7efc23c87284 to your computer and use it in GitHub Desktop.
A very customizable FizzBuzz solution
<?php
$divider_flags = [
3 => "three",
5 => "five",
7 => "hello"
];
# Order of this array is preserved
$flag_correspondences = [
"hello" => "Scrumptious",
"three" => "Fizz",
"five" => "Buzz",
];
for ($i = 1; $i < 22; $i++) {
$flags = [];
foreach ($divider_flags as $divider => $flag) {
if ($i % $divider == 0) {
array_push($flags, $flag);
}
}
$text = join("", array_intersect_key($flag_correspondences, array_flip($flags)));
if (empty($text)) {
$text .= $i;
}
echo $text . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment