Skip to content

PHP Programs 7

Print Fibonacci series up to n numbers e.g. 0 1 1 2 3 5 8 13 21…..n

<?php
function printFibonacciNumbers($n)
{
	$f1 = 0;
	$f2 = 1;
	$i;

	if ($n < 1)
		return;
	echo($f1);
	echo(" ");
	for ($i = 1; $i < $n; $i++)
	{
		echo($f2);
		echo(" ");
		$next = $f1 + $f2;
		$f1 = $f2;
		$f2 = $next;
	}
}
	printFibonacciNumbers(15);
?>

Output