Skip to content

Instantly share code, notes, and snippets.

@soldev-42
Created November 3, 2015 19:46
Show Gist options
  • Select an option

  • Save soldev-42/6384f8ff046568c2df16 to your computer and use it in GitHub Desktop.

Select an option

Save soldev-42/6384f8ff046568c2df16 to your computer and use it in GitHub Desktop.
<?php
/**
* @param {Integer} $n number of fibonacci sequence items
* @return {String} fibonacci numbers calculated using recursion
*/
function fibonacciRec($n, $a = array()){
if(!isset($a[0]) || $a[0] != 1){
$a[0] = 1;
}
if(!isset($a[1]) || $a[1] != 1){
$a[1] = 1;
}
$t = count($a);
$a[] = $a[$t - 1] + $a[$t - 2];
if($n > count($a)){
return fibonacciRec($n, $a);
}
return implode(', ', $a);
}
echo 'Recursive fibonacci '.fibonacciRec(10)."\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment