PHP - Function Recursive Functions

Introduction

A recursive function calls itself.

The following code uses recursive function to output fibonacci numbers.

Demo

<?php
        $iterations = 10;// w  ww.jav  a  2  s.c om
        function fibonacci($n) {
            if (($n == 0) || ($n == 1)) 
               return $n;
            return fibonacci($n-2) + fibonacci($n-1);
        }

        for ($i=0; $i <= $iterations; $i++){
             echo $i;
             echo "\n";
             echo fibonacci($i);
             echo "\n";
        }
?>

Result