Home >>PHP Programs >PHP Program to print the Fibonacci series

PHP Program to print the Fibonacci series

PHP Program to print the Fibonacci series

In this example, we will create a PHP program to print the Fibonacci series.

If the numbers in a series is the sum to its previous two numbers then it is said to be a Fibonacci series.

Fibonacci Series in PHP Example 1

<?php  
$num = 0;  
$n1 = 0;  
$n2 = 1;  
echo "<h3>Fibonacci series for first 12 numbers: </h3>";  
echo "\n";  
echo $n1.' '.$n2.' ';  
while ($num < 10 )  
{  
    $n3 = $n2 + $n1;  
    echo $n3.' ';  
    $n1 = $n2;  
    $n2 = $n3;  
    $num = $num + 1;  
}

?> 

Output:-
Fibonacci series for first 12 numbers:
0 1 1 2 3 5 8 13 21 34 55 89
Fibonacci Series in PHP Example 2

<?php 
extract($_REQUEST);
if(isset($check))
{
$num = 0;  
$n1 = 0;  
$n2 = 1;  
echo "<h3>Fibonacci series for first $n numbers: </h3>";  
echo "\n";  
echo $n1.' '.$n2.' ';  
while ($num < $n-2 )  
{  
    $n3 = $n2 + $n1;  
    echo $n3.' ';  
    $n1 = $n2;  
    $n2 = $n3;  
    $num = $num + 1;  
}
}
?>
<!DOCTYPE html>
<html>
	<head>
		<title>Form</title>
	</head>
	<body>
		<form method="post">
		<table>
		<tr>
		<td>Enter Your Number</td>
		<td><input type="text" name="n" required/></td>
		</tr>
		<td colspan="2" align="center">
			<input type="submit" value="Print Fibonacci Series" name="check"/>
		</tr>
		</form>
	</body>
</html>
</html>

Output:-
Print fibonacci series

No Sidebar ads