What is recursion in PHP?

22 Views

What is recursion in PHP?

Recursion 

PHP also supports recursive function call like C/C++. In such case, a function calls itself within 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  

 

Tags:-
PHP