Fibonacci series is a such kind of series of numbers in which the current number is the addition of previous two numbers. It is a mathematical series. Today we will see How to make Fibonacci series by PHP programming without using array or function.
Procedure:- We will collect the 1st number, 2 number and the length of series by HTML.
- Then we will print them serially.
- Add them. It will be the third number.
- Print the third number and generate the next number.
- Use a loop till the length ends. It will generate the Fibonacci Sequence.
First generate the HTML to read data.
<form action='?' method='POST'> Enter the startig number: <input name="start" /><br/> Enter the second number: <input name="second" /><br/> Enter the series length: <input name="length" /><br/> <input type='submit' value='Generate Fibonacci'/> </form>
Now do the operations.
<?php if(isset($_POST['start'])) { $start = $_POST['start']; $length = $_POST['length']; $second = $_POST['second']; print "<u><b>The generated fibonacci series is:</b></u> " . $start ."," . $second; $now = $start + $second; for($i=3;$i<=$length;$i++) { print "," . $now; $cache = $now; $now = $now + $second; $second = $cache; } print "..."; } ?>
Happy programming!