You are on page 1of 3

Page |1

Calculating the Fibonacci number with PHP and HTML

Document version of a www.betaprogrammer.com article by Jake DiMare-


August 18, 2010

In this article we’ll take a look at some very basic programming with math
using PHP and HTML to calculate the Fibonacci number. The sequence of the
Fibonacci number is calculated by starting with 1+2=3 and then continuing on
by adding the sum to the preceding number after each pair. For example,
1+2=3, 2+3=5, 3+5=8, etc.

The goal of this exercise is to learn a few things about loops, variables and
processing forms with PHP. This is a very simple exercise and the intended audience are those new to
programming or, at least, new to PHP. We’ll go through a couple of iterations showing how this can be
done statically before moving onto something more dynamic that allows the user to enter a value with a
form to increase the size of the sequence.

The Fibonacci number has been fascinating mankind as far back as ancient India when it was used for
metric sciences. Architects in the Roman Empire referred to it as the “Golden Mean”. It appears in
nature, for instance in the growth of plant life or the spiraling of certain kinds of shells. It is also woven
into the song Lateralus, by my favorite band, Tool.

In the first example, let’s explore how this is done statically. Calculating the sequence with programming
is actually very simple math because we can use a loop function to handle the recursion. Thus, we simply
need to keep doing the calculation over and over again as many times as we want and each time change
the value of the two variables being added together. Consider the following code, where we use a while
loop to introduce a static limit on the number of recursions. In this case, so long as the value of Z is less
than 1000.

The fibonacci sequence is: <br />1<br />2<br />


<?php
$x = 1;
$y = 2;
while($z < 1000) {
    $z = $x + $y;
    echo($z."<br />");
    $x = $y;
    $y = $z;
}
?> 

The preceding code produces the following results:

http://betaprogrammer.com/fibonacci/index2.php (link to the example).

Calculating the Fibonacci number with PHP and HTML – www.betaprogrammer.com


Page |2

Pretty simple really. Place the calculation in a loop and each time it runs shift the values of x and y to be
y and z. In order to limit the number of times this runs enter a number in the while statement. But then I
got to thinking…What if we want to see the sequence out much, much further? It would be simple to
just increase the value in the while loop but eventually it is going to get ridiculously large and
impractical. Therefore, I replaced the number 1000 with a new variable, called $max

In the next example we add in a way to calculate exponential values for $max, allowing us to quickly and
easily create some pretty large numbers.

The fibonacci sequence is: <br />1<br />2<br />

<?php
$powerof = 3;
$max = 10;
$temp = $max;
$i = 1;
for($i;$i<$powerof;$i++){
    $max = $max * $temp;
}
$x = 1;
$y = 2;
while($z < $max) {
    $z = $x + $y;
    echo($z."<br />");
    $x = $y;
    $y = $z;
}
?>

http://betaprogrammer.com/fibonacci/index1.php (link to the example).

In this case we calculate the $max limit for the sequence of Fibonacci numbers by using a for loop to
multiply $max by itself $powerof times. Because we made the initial value of $max 10 and the value of
$powerof 3 it equals the same amount as the previous example, 1000.

From here it is a very logical step to add a form. This could be done any number of ways but in the next
example our form field will allow us to enter in whatever value we want for $powerof while leaving the
value of $max at 10. In essence, we will be allowed to determine the power of 10.

<?php
$message = ‘The fibonacci sequence is: <br />1<br />2<br />’;
$powerof = 0;
$max = 10;
$temp = $max;
Calculating the Fibonacci number with PHP and HTML – www.betaprogrammer.com
Page |3

if(isset($_POST['power'])) {
    $powerof = $_POST['power'];}else{$powerof = 2;
}
if($powerof > 100) {
    $powerof = 100;
    $message = ‘Sorry, your input was too high. I converted it to the maximum value of 100.<br />The
fibonacci sequence is: <br />1<br />2<br />’;
}
$i = 1;
for($i;$i<$powerof;$i++){
    $max = $max * $temp;
}
$x = 1;
$y = 2;
echo($message);
while($z < $max) {
    $z = $x + $y;
    echo($z."<br />");
    $x = $y;
    $y = $z;
}
?>

 Notice the two if statements? The first was added to create a default value, in the event the form has
not been run yet. The second one enforces a maximum value for $powerof. Sadly, this is necessary on a
web application because without it eventually a high enough value passed will take the server longer to
process than is allowed by the timeout restriction for the page.

http://betaprogrammer.com/fibonacci/index.php (link to the example).

Calculating the Fibonacci number with PHP and HTML – www.betaprogrammer.com

You might also like