Implementing Fibonacci numbers using recursion

In mathematics, Fibonacci numbers are special integer sequences where a number is composed from summation of the past two numbers, as shown in the following the expression:

If we implement this using PHP 7, it will look like this:

function fibonacci(int $n): int { 
if ($n == 0) {
return 1;
} else if ($n == 1) {
return 1;
} else {
return fibonacci($n - 1) + fibonacci($n - 2);
}
}

If we consider the preceding implementation, we can see it is a bit different from the previous examples. Now, we are calling two functions from one function call. We will discuss different types of recursions shortly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset