PHP Recursive Functions

Introduction of Recursive Functions:

PHP also supports recursive function call like C++/C and other languages. In such a case. A recursive function is a function which calls itself to repeat, again and again until the final condition arrives. Recursion is one of those topics that can seem confusing at first, but once you start writing recursive functions you’ll see how elegant recursion can be!. Recursion can be thought of as an alternative to iteration — that is, for and whileloops.

<?phpfunction printNumber($i){//Breaking Condition   if ($i>5) {    return;   }  echo $i."\n";  $i++;  printNumber($i);}printNumber(1);



At first, you need to complete the breaking condition part. If you forget to complete the “Breaking Condition” or false Condition Then you will fall into an infinite loop. So always try to complete the “Breaking Condition” part First. It will help you to reduce some bugs. This program will be printing the number from one to Five, In this Code, the function used to print the number is the name printNumber() and the $i is the name of the variable that will help the printNumber() function get some value that will eventually lead to invoking it. If the statement is used to perform the condition checking. The program Shall keep on executing until the fixed value is stored in the $i variable is less than one. When the value stored in it exceeds the value of five, the condition that must be satisfied to execute the program further will go false, and the program will be terminated.

In the previous program, we learned how to leverage recursion to display the number. Now in this program, we will learn how to change the logic of the application to find the factorial. Before we start to write code for calculating Factorial, it is necessary to understand what is factorial. Factorial of any number is the value which is obtained by reducing the number by one and then multiplying the outcome by the number, and it has to be repeated till one. For example, if we need to calculate the factorial of 5, it can be calculated using 5*4*3*2*1. So the outcome will be 120. In the below program, the value will be given in the program. The program will process the value to calculate the outcome of the factorial. The value will be passed through the function, and then all the logic is written will be fixed on it to calculate the outcome. Below is the program, so let’s proceed to have a look at it.

function factorial($number) {    if ($number == 0) {       return 1;     }    return $number * factorial($number - 1);  }  echo factorial(5);



The above-written code is the implementation of the factorial using PHP. The name of the function is factorial() that will be used to calculate the factorial. The function with the same name has been called within it that is used to implement the mechanism of factorial in the program. $number is the variable that will be storing the value of which we have to find the factorial. We have used the IF condition checking to make sure that it is meeting the requirements that are considered essential when it comes to calculating the factorial of any value. In the very last line, the main call of the factorial() function has been done that has invoked the functionality defined in this function. At this time, we have passed four as we wanted to calculate the factorial of four. In case if you want to try this code with different values and replace the $number 4 in this program with the value of which you want to find the factorial.

Web Application Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store