Press "Enter" to skip to content

PHP Recursion Example

What is Recursion?

Recursion is a process where a function calls itself repeatedly. The recursive function can contain a condition that will stop the recursion after the desired result has been found. A recursion can lead to an infinite loop if the base case is not met in the calls. In this tutorial, I am giving a PHP recursion example.

Here is an example for implementing the recursion in PHP

<html>
    <body>
        <?php
function f1()
{
    echo "welcome ";
    f1();
}
f1();//function calling
        ?>
    </body>
</html>

Output

welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome

In this program, we have used a function f1(), which is calling repeatedly and function body is executing continuously with the printing of “welcome”.

Here is a PHP Program for calculating the factorial of a number using recursion

The factorial of a positive number is the product of positive integers less than or equal to that number. The factorial of a positive integer n is denoted by n! and n!=1*2*3*……*(n-1)*n.

<html>
    <body>
        <?php
function f1($a)
{
    if($a<=0) //base case
        return(1);
    else
        return($a*f1($a-1));
}
$b=5;
$t=f1($b);//factorial value to $t
echo "The factorial of the number $b using recursion is=".$t;
        ?>
    </body>
</html>

Output

The factorial of the number 5 using recursion is=120.

In the above program, we have defined a function with a parameter $a. The function body is containing a base case when $a will be less than or equal to 0 then function will return 1, and this value will be returned to the code that called it. If the base case is not met, the function will be called itself with $n-1 as an argument and then multiplies the resulting value by $n.Finally, the value will be returned to the calling code, and we will get the factorial.

Here is another example using a recursion method:

In this program, we will calculate the greatest common divisor of two numbers using a recursion method. We are using a user-defined function here f1() with two parameters $a and $b, and we are using two variables as $b1=5 and $b2=10. We are passing these values as an argument to $a and $b in the time of calling the function f1.

<html>
    <body>
        <?php
function f1($a,$b)
{
    if($a==$b) //base case
        return($a);
    if($a>$b) //condition 1
        return(f1($a-$b,$b));
    if($a<$b) //condition 2
        return(f1($a,$b-$a));
}
$b1=5; //value of first number
$b2=10; //value of second number
$t=f1($b1,$b2);
echo "The GCD of two numbers $b1 and $b2 using recursion is=".$t;
        ?>
    </body>
</html>

Output

The GCD of two numbers 5 and 10 using recursion is=5

Here in the above program, if the base case is not satisfied then there are two conditions (here condition1 and condition2). Based on the satisfaction of any condition, function will be called itself with arguments either ($a-$b,$b) or ($a,$b-$a) and then f1 function will return the gcd of two numbers to $t. Finally, we will get the result.

Calculating the value of a to the power (b) using recursion method:

In this program we are using a base case here($b==0). If the base case is not satisfied then function here f1, will be called itself with $a and $b-1 as arguments and then multiplies the resulting value by $a.

<html>
    <body>
        <?php
function f1($a,$b)
{
    if($b==0)
        return(1);
    else
        return($a*f1($a,$b-1));}
$t=f1(5,3);
echo "The output is=".$t;
        ?>
    </body>
</html>

Output

The output is=125

Here we have passed two values(5,3)as arguments to two formal parameters $a and $b in the time of function call. After the execution of the function body, a function will return a value to $t and we get the desired result(here 125).