3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * @file A fizzbuzz reference implementation. * @author Brad Czerniak (ao5357@gmail.com) */ /** * Outputs fizzes and buzzes over a loop. * * @param int $iterations * The number of times to iterate over the loop * @param int $offset * The number to start at * @param string $fizz * The string to output for the first modulo condition, typically 'fizz'. * @param string $buzz * The string to output for the second modulo condition, typically 'buzz'. * @param string $fizzbuzz * The string to output for the third (usually combined) modulo condition, typically 'fizzbuzz'. * @param int $fizz_int * The first modulus * @param int $buzz_int * The second modulus * @param int $fizzbuzz_int * The third (usually combined) modulus * * @return An array of the loop's output */ function fizzbuzz($iterations = 100, $offset = 1, $fizz = 'fizz', $buzz = 'buzz', $fizzbuzz = 'fizzbuzz', $fizz_int = 3, $buzz_int = 5, $fizzbuzz_int = 15) { $i = $offset; $ret = array(); while($i < ($offset + $iterations)){ if($i % $fizzbuzz_int == 0){ $ret[$i] = $fizzbuzz;} elseif($i % $buzz_int == 0){ $ret[$i] = $buzz;} elseif($i % $fizz_int == 0){ $ret[$i] = $fizz;} else{ $ret[$i] = $i;} $i++; } return $ret; } print_r(fizzbuzz());
Output for 4.3.0 - 7.1.0
Array ( [1] => 1 [2] => 2 [3] => fizz [4] => 4 [5] => buzz [6] => fizz [7] => 7 [8] => 8 [9] => fizz [10] => buzz [11] => 11 [12] => fizz [13] => 13 [14] => 14 [15] => fizzbuzz [16] => 16 [17] => 17 [18] => fizz [19] => 19 [20] => buzz [21] => fizz [22] => 22 [23] => 23 [24] => fizz [25] => buzz [26] => 26 [27] => fizz [28] => 28 [29] => 29 [30] => fizzbuzz [31] => 31 [32] => 32 [33] => fizz [34] => 34 [35] => buzz [36] => fizz [37] => 37 [38] => 38 [39] => fizz [40] => buzz [41] => 41 [42] => fizz [43] => 43 [44] => 44 [45] => fizzbuzz [46] => 46 [47] => 47 [48] => fizz [49] => 49 [50] => buzz [51] => fizz [52] => 52 [53] => 53 [54] => fizz [55] => buzz [56] => 56 [57] => fizz [58] => 58 [59] => 59 [60] => fizzbuzz [61] => 61 [62] => 62 [63] => fizz [64] => 64 [65] => buzz [66] => fizz [67] => 67 [68] => 68 [69] => fizz [70] => buzz [71] => 71 [72] => fizz [73] => 73 [74] => 74 [75] => fizzbuzz [76] => 76 [77] => 77 [78] => fizz [79] => 79 [80] => buzz [81] => fizz [82] => 82 [83] => 83 [84] => fizz [85] => buzz [86] => 86 [87] => fizz [88] => 88 [89] => 89 [90] => fizzbuzz [91] => 91 [92] => 92 [93] => fizz [94] => 94 [95] => buzz [96] => fizz [97] => 97 [98] => 98 [99] => fizz [100] => buzz )