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(200, 6, 'something', 'stuff', 'darkside', 5, 7, 17));
Output for 4.3.0 - 7.1.0
Array ( [6] => 6 [7] => stuff [8] => 8 [9] => 9 [10] => something [11] => 11 [12] => 12 [13] => 13 [14] => stuff [15] => something [16] => 16 [17] => darkside [18] => 18 [19] => 19 [20] => something [21] => stuff [22] => 22 [23] => 23 [24] => 24 [25] => something [26] => 26 [27] => 27 [28] => stuff [29] => 29 [30] => something [31] => 31 [32] => 32 [33] => 33 [34] => darkside [35] => stuff [36] => 36 [37] => 37 [38] => 38 [39] => 39 [40] => something [41] => 41 [42] => stuff [43] => 43 [44] => 44 [45] => something [46] => 46 [47] => 47 [48] => 48 [49] => stuff [50] => something [51] => darkside [52] => 52 [53] => 53 [54] => 54 [55] => something [56] => stuff [57] => 57 [58] => 58 [59] => 59 [60] => something [61] => 61 [62] => 62 [63] => stuff [64] => 64 [65] => something [66] => 66 [67] => 67 [68] => darkside [69] => 69 [70] => stuff [71] => 71 [72] => 72 [73] => 73 [74] => 74 [75] => something [76] => 76 [77] => stuff [78] => 78 [79] => 79 [80] => something [81] => 81 [82] => 82 [83] => 83 [84] => stuff [85] => darkside [86] => 86 [87] => 87 [88] => 88 [89] => 89 [90] => something [91] => stuff [92] => 92 [93] => 93 [94] => 94 [95] => something [96] => 96 [97] => 97 [98] => stuff [99] => 99 [100] => something [101] => 101 [102] => darkside [103] => 103 [104] => 104 [105] => stuff [106] => 106 [107] => 107 [108] => 108 [109] => 109 [110] => something [111] => 111 [112] => stuff [113] => 113 [114] => 114 [115] => something [116] => 116 [117] => 117 [118] => 118 [119] => darkside [120] => something [121] => 121 [122] => 122 [123] => 123 [124] => 124 [125] => something [126] => stuff [127] => 127 [128] => 128 [129] => 129 [130] => something [131] => 131 [132] => 132 [133] => stuff [134] => 134 [135] => something [136] => darkside [137] => 137 [138] => 138 [139] => 139 [140] => stuff [141] => 141 [142] => 142 [143] => 143 [144] => 144 [145] => something [146] => 146 [147] => stuff [148] => 148 [149] => 149 [150] => something [151] => 151 [152] => 152 [153] => darkside [154] => stuff [155] => something [156] => 156 [157] => 157 [158] => 158 [159] => 159 [160] => something [161] => stuff [162] => 162 [163] => 163 [164] => 164 [165] => something [166] => 166 [167] => 167 [168] => stuff [169] => 169 [170] => darkside [171] => 171 [172] => 172 [173] => 173 [174] => 174 [175] => stuff [176] => 176 [177] => 177 [178] => 178 [179] => 179 [180] => something [181] => 181 [182] => stuff [183] => 183 [184] => 184 [185] => something [186] => 186 [187] => darkside [188] => 188 [189] => stuff [190] => something [191] => 191 [192] => 192 [193] => 193 [194] => 194 [195] => something [196] => stuff [197] => 197 [198] => 198 [199] => 199 [200] => something [201] => 201 [202] => 202 [203] => stuff [204] => darkside [205] => something )