Recursion
Recursion is a method or a function which calls itself.
While the method calls itself, it also must have a branch where it does not call itself: otherwise, the call will never end and result in infinite recursion.
Functions, methods, closures and arrow functions may be recursive.
Recursion is an abstract notion. It is also used in the PHP name itself: PHP: Hypertext Preprocessor.
<?php
function factorial(int $number) {
if ($number < 2) {
return 1;
}
return ($number * factorial($number - 1));
}
// recursive closure
$factorial = function( $n ) use ( &$factorial ) {
if( $n == 1 ) return 1;
return $factorial( $n - 1 ) * $n;
};
?>