PHP program to print nth number of Fibonacci Series.

Fibonacci Series: 0 1 1 2 3 5 8 13 21 …

Here are 2 PHP programs to print nth number of Fibonacci Series:

Iterative Program to find nth number of Fibonacci Series:

<?php

fibonacci(1);
fibonacci(2);
fibonacci(3);
fibonacci(4);
fibonacci(5);

function fibonacci($n) 
{
        $first_num = 0;
	$second_num = 1;

	if(1 == $n)
	{
		echo $first_num;
	}
	elseif(2 == $n)
	{
		echo $second_num;
	}
	else
	{
		$next_num = 0;
		for($i=1; $i<=$n-2; $i++)
		{
			$next_num = $first_num + $second_num;
			$first_num = $second_num;
			$second_num = $next_num;
		}
		echo $next_num;
	}
}

?>

Recursive Program to find nth number of Fibonacci Series:

<?php

echo fibonacci(1);
echo fibonacci(2);
echo fibonacci(3);
echo fibonacci(4);
echo fibonacci(5);

function fibonacci($n) 
{
        if(1 == $n)
	{
		$result = 0;
	}
	elseif(2 == $n)
	{
		$result = 1;
	}
	else
	{
		$result = fibonacci($n-1) + fibonacci($n-2);
	}
	return($result);
}

?>