ifnull
8/3/2012 - 11:31 PM

FizzBuzz in PHP

FizzBuzz in PHP

<?php
/*
 * Requirements: No math functions, get single iteration, get iteration range and get infinite iterations
 * $start: Starting iteration
 * $count: Number of iterations with "0" being infinite. 
 */

$start = 10;
$count = 0;

$ugh = array_fill(0, 15, '');
$fizz = array('', '', 'fizz');
$buzz = array('','','','','buzz');

foreach ($ugh as &$val) {
    $val = current($fizz).current($buzz);
	if(next($fizz) === false){
		reset($fizz);
	}
	if(next($buzz) === false){
		reset($buzz);
	}
}

reset($ugh);

$i = 1;
$lemniscate = (boolean) $count;
while ($i < $start+$count || !$lemniscate) {
	if($i >= $start){
		print(sprintf('%d: %s', $i, current($ugh))."\n");
	}
	if(next($ugh) === false){
		reset($ugh);
	}
	$i++;
}