What is recursion
Recursion: A programming method in which a function calls itself, similar to a loop, so the function called recursively must There is a termination condition, otherwise it will become an infinite loop.
Commonly used methods of recursion:
1. Static variable method
function loop(){ static $i = 0; echo $i.' '; $i++; if($i<10){ loop(); } } loop();//输出 0 1 2 3 4 5 6 7 8 9
2. Global variable method
$i = 0; function loopGlobal(){ global $i; echo $i.' '; $i++; if($i<10){ loopGlobal(); } } loopGlobal();//输出 0 1 2 3 4 5 6 7 8 9
3. Reference parameter passing method
function loopReference(&$i=0){ echo $i.' '; $i++; if($i<10){ loopReference($i); } } loopReference();//输出 0 1 2 3 4 5 6 7 8 9
Recommended tutorial: PHP tutorial
The above is the detailed content of What is recursion in PHP? What are the ways to achieve this?. For more information, please follow other related articles on the PHP Chinese website!