Site icon T4Tutorials.com

PHP program to find the Fibonacci series with form and database

PHP program to find the Fibonacci series with form and database

In this tutorial, we will learn about the followings;

  1. PHP program to find the Fibonacci series with form and database
  2. PHP program to find the Fibonacci series with form and database
  3. PHP program to find the Fibonacci series with form and database

PHP program to find the Fibonacci series.

In this example, programmer stores the values in the variables. However, if you interested to see the program about taking values in the form, please see example 2.

<html>
<body>
</body>
</html>
<?php
function F($n)
 {
  $number1 = 0;
  $number2 = 1;
  echo "Fibonacci Series \n";
  echo $number1.' '.$number2.' ';
  for($i = 2; $i < $n; $i++){
    $number3 = $number1 + $number2;
    echo $number3.' ';
    $number1 = $number2;
    $number2 = $number3;
    }
}
F(2);
?>

PHP program to find the Fibonacci series with form.

In this example, we will take the inputs from the user and store them in variables.

<html>
<body>
  <form method="post">
    <input type="text" name="Number" >
  <button name="submit">Submit</button>
  </form>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
  $n=$_POST['Number'];
  $number1 = 0;
  $number2 = 1;
  echo "Fibonacci Series \n";
  echo $number1.' '.$number2.' ';
  for($i = 2; $i < $n; $i++){
    $number3= $number1 + $number2;
    echo $number3.' ';
    $number1 = $number2;
    $number2 = $number3;
    
  }
}
    ?>

PHP program to find the Fibonacci series with the database.

In this example, we will take the inputs from the user and temporarily store them in variables and then finally stores them permanently in the database.

<html>
<body>
<form method="post">
	<input type="text" name="Number" >
	<button name="submit">Submit</button>
</form>
</body>
</html>
<?php
$conn = mysqli_connect('localhost','root','','fibonacci');
if (isset($_POST['submit'])) {
  $n=$_POST['Number'];
  $number1 = 0;
  $number2 = 1;
  echo "Fibonacci Series \n";
  echo $number1.' '.$number2.' ';
  for($i = 2; $i < $n; $i++){
    $number3= $number1 + $number2;
    echo $number3.' ';
    $number1 = $number2;
    $number2 = $number3;
    $temp=' '.$number3;
    }
    $sql= "INSERT INTO  fib (result) VALUES('$temp')";
	 if($conn->query($sql)=== TRUE)
{
	echo"<font color=#FF00FF> New Record Inserted<br></font>";
	}
  else
  {
	  echo"Error:".$sql."<br>".$conn->error;
	  }}
//function F($n)
 //{
  
//}
//F(2);
?>

Database to find the Fibonacci series

Figure: Fibonacci series program in php.

 

Exit mobile version