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[] = $fizzbuzz;} elseif($i % $buzz_int == 0){ $ret[] = $buzz;} elseif($i % $fizz_int == 0){ $ret[] = $fizz;} else{ $ret[] = $i;} $i++; } return $ret; } print_r(fizzbuzz());
Output for 4.3.0 - 7.1.0
Array ( [0] => 1 [1] => 2 [2] => fizz [3] => 4 [4] => buzz [5] => fizz [6] => 7 [7] => 8 [8] => fizz [9] => buzz [10] => 11 [11] => fizz [12] => 13 [13] => 14 [14] => fizzbuzz [15] => 16 [16] => 17 [17] => fizz [18] => 19 [19] => buzz [20] => fizz [21] => 22 [22] => 23 [23] => fizz [24] => buzz [25] => 26 [26] => fizz [27] => 28 [28] => 29 [29] => fizzbuzz [30] => 31 [31] => 32 [32] => fizz [33] => 34 [34] => buzz [35] => fizz [36] => 37 [37] => 38 [38] => fizz [39] => buzz [40] => 41 [41] => fizz [42] => 43 [43] => 44 [44] => fizzbuzz [45] => 46 [46] => 47 [47] => fizz [48] => 49 [49] => buzz [50] => fizz [51] => 52 [52] => 53 [53] => fizz [54] => buzz [55] => 56 [56] => fizz [57] => 58 [58] => 59 [59] => fizzbuzz [60] => 61 [61] => 62 [62] => fizz [63] => 64 [64] => buzz [65] => fizz [66] => 67 [67] => 68 [68] => fizz [69] => buzz [70] => 71 [71] => fizz [72] => 73 [73] => 74 [74] => fizzbuzz [75] => 76 [76] => 77 [77] => fizz [78] => 79 [79] => buzz [80] => fizz [81] => 82 [82] => 83 [83] => fizz [84] => buzz [85] => 86 [86] => fizz [87] => 88 [88] => 89 [89] => fizzbuzz [90] => 91 [91] => 92 [92] => fizz [93] => 94 [94] => buzz [95] => fizz [96] => 97 [97] => 98 [98] => fizz [99] => buzz )