Change language

Anonymous recursive function in 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 



                   
                
                

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically