Friends, This is another school / college script for you. This php program could be useful for the Freshers who are searching for job as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<!DOCTYPE html> <html> <head> <title>PHP Fibonacci Script</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <h2>PHP script to print Fibonacci series</h2> <h3>Enter the limit:</h3> <form method="post"> <input type="text" name="limit" /> <button name="submit" type="submit" >Print Fibonacci Series</button> </form> <br /> </body> </html> <?php if(isset($_POST['submit'])){ $limit = 0; if(isset($_POST['limit'])){ $limit = $_POST['limit']; } $first_num = 0; $second_num = 1; $next_num = 0; //check the input if the value is greater than 0 if($limit > 0){ print $first_num . "<br>"; }else{ print "Value should be greater than 0"; } //check if input is 1 if($limit > 1){ print $second_num . "<br>"; } //if the input is > 2 then do then print fibonacci while($limit > 2) { //add the first value and second to get the next value $next_num = $first_num + $second_num; //assign second value to first //next value to second //to find the next series $first_num = $second_num; $second_num = $next_num; //print the next value print $next_num . "<br>"; //decrement the limit by one $limit--; } } ?> |
Thing you will learn from the above program will be:
- How to get the POST input value
- How to write While loop in PHP
- Logic for fibonacci series
Output of the above Fibonacci will be here:
And you can see the demo in here:
Enjoy the day!