PHP - Function Recursive Functions

Introduction

A recursive function calls itself.

The following code uses recursive function to output fibonacci numbers.

Demo

<?php
        $iterations = 10;// www.  j  av  a 2 s .  c  o  m
        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