Create Fibonacci sequence in PHP

Description

The following code shows how to create Fibonacci sequence.

Example


<!DOCTYPE html>// ww w  . j a  v a2s  .  c  om
<html>
  <body>
    <table>
      <tr>
        <th>Sequence #</th>
        <th>Value</th>
      </tr>
      <tr>
        <td>F<sub>0</sub></td>
        <td>0</td>
      </tr>
      <tr class="alt">
        <td>F<sub>1</sub></td>
        <td>1</td>
      </tr>
        <?php
        
        $iterations = 10;
        
        $num1 = 0;
        $num2 = 1;
        
        for ( $i=2; $i <= $iterations; $i++ )
        {
          $sum = $num1 + $num2;
          $num1 = $num2;
          $num2 = $sum;
        ?>
              <tr<?php if ( $i % 2 != 0 ) echo ' class="alt"' ?>>
                <td>F<sub><?php echo $i?></sub></td>
                <td><?php echo $num2?></td>
              </tr>
        <?php
        }
        ?>
    </table>
  </body>
</html>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Development »




Environment
Error
Hash
Include
Locale
Math
Network
Output
Reflection
PHP Regular Expressions