Anonymous recursive function in PHP

PHP

Using anonymous recursion:
  • Anonymous recursion is mainly used to allow recursion for anonymous functions.
  • Especially when they form closures or are used as callbacks to avoid having to bind the function name.
Alternatives :
  • Using named recursion and named functions.
  • If an anonymous function is specified, anonymous recursion can be performed by associating the name with the function, as in named functions.
Program 1:
// PHP illustration program
// Anonymous recursive function $func = function ( $limit = NULL) use (& amp; $func ) { static $current = 10; // if the condition for checking the value of $current. if ( $current < = 0) { // breaking recursion return FALSE; } // Display the value of $current. echo "$current" ; $current -; $func (); };  
// function call $func (); ?>
Exit:
10 9 8 7 6 5 4 3 2 1
Program 2:
// PHP illustration program
// Anonymous recursive function $factorial = function ( $num ) use (& amp; $factorial ) { // Basic recursion condition  if ( $num == 1) return 1 ; // return a statement when $m is not 1. return $factorial ( $num - 1) * $num ; };  
// function call print $factorial (6); ?>
Exit:
720