What is recursion in PHP?

Last updated 5 years, 8 months ago | 2152 views 75     5

Tags:- PHP

PHP | Recursion 

PHP also supports recursive function call like C/C++. In such a case, a function calls itself within the function.

 

Note: Recursion must be incorporated carefully since it can lead to an infinite loop if no condition is met that will terminate the function.

<?php   

function display($num) {

    if($num<=5){    

    echo $num.”,”;    

    display($num+1);   //function call itself.
    }

}      

display(1);  

?>  

//Output will be 1, 2, 3, 4, 5