69. PHP Program to find the Fibonacci series of a given number.
How does this program work?
- This program is used to find the Fibonacci series of a given number using PHP.
- Fibonacci series means the previous two elements are added to get the next element starting with 0 and 1.
- First we initializing first and second number as 0 and 1, and print them.
- The third number will be the sum of the first two numbers by using loop.
Here is the code
<html>
<head>
<title>PHP Program To find the Fibonacci series of a given number</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num1" value="" placeholder="Enter a number"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$n = $_POST['num1'];
$num1 = 0; //Intialise Variables
$num2 = 1;
echo "Fibonacci series of $n :";
echo $num1.' '.$num2;
//Loop to print Fibonacci series
for($i = 2; $i < $n; $i++)
{
$num3 = $num1 + $num2 ;
echo ' '.$num3 ;
$num1 = $num2 ;
$num2 = $num3;
}
return 0;
}
?>
</body>
</html>