Home >>PHP Programs >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.
<?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;
}
?>
<?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>