What is Recursive function in PHP ?

In Short Recursive function is a function which call itself or themselves during the execution of the program .

Note **
Every Recursive function must need to have the termination condition .
The function must call the function itself and each recursive call must have different than the previous one .

Let Us consider the practical example for the Recursive function :

<?php
//recursive factorial function

function factorial_number($number) {

if ($number < 2) {
return 1;
} else {
return ($number * factorial_number($number-1));
}
}
echo factorial_number(5)

?>

From the above example , we are calculating the factorial of 5 which means multiplication of number from 1 to 5 i.e 5*4*3*2*1 , The the out put for the above function will be 120

Leave a Reply

Your email address will not be published.Required fields are marked *