Write a program to print Fibonacci series
in the PHP Language
Answers
★彡 Here is Your Answer 彡★
<?php
if(isset($_POST['sub']))
{ $fibo=array(0,1);
$num=$_POST['nm1'];
for($i=2;$i<=$num-1;$i++)
{
$fibo[$i]=$fibo[$i-1]+$fibo[$i-2];
}
}
?>
<html>
<head>
<title>Fibonacci Series</title>
</head>
<body>
<table>
<form name="frm" method="post" action="">
<tr><td>Number of Terms:</td><td><input type="text" name="nm1" /></td></tr>
<tr><td></td><td><input type="submit" name="sub" /></td>
<td><center><span>
<?php if(isset($_POST['sub'])){
for($i=0;$i<=$num-1;$i++){echo $fibo[$i].",";}
}
?>
</span></center>
</td></tr>
</form>
</table>
</body>
</html>
Ehsass 彡★
We'll show an example to print the first 12 numbers of 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;
?>
Output:
PHP Fibonacci series 1
Fibonacci series using Recursive function
Recursion is a phenomenon in which the recursion function calls itself until the base condition is reached.
<?php
/* Print fiboancci series upto 12 elements. */
$num = 12;
echo "<h3>Fibonacci series using recursive function:</h3>";
echo "\n";
/* Recursive function for fibonacci series. */
function series($num){
if($num == 0){
return 0;
}else if( $num == 1){
return 1;
} else {
return (series($num-1) + series($num-2));
}
}
/* Call Function. */
for ($i = 0; $i < $num; $i++){
echo series($i);
echo "\n";
}