Anonymous recursive function in PHP Last Updated : 28 Nov, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report Anonymous recursive function is a type of recursion in which function does not explicitly call another function by name. This can be done either comprehensively, by using a higher order function passing in a function as an argument and calling that function. It can be done implicitly, via reflection features which allow one to access certain functions depending on the current context, especially ,the current function. In theory of computer science, anonymous recursion is significant, as anonymous recursion is type of recursion in which one can implement recursion without requiring named functions . Use of Anonymous Recursion: Anonymous recursion is primarily of use in allowing recursion for anonymous functions. Particularly when they form closures or are used as callbacks, to avoid having to bind the name of the function. Alternatives: The of use named recursion and named functions. If an anonymous function is given,anonymous recursion can be done either by binding a name to the function, as in named functions. Program 1: php <?php // PHP program to illustrate the // Anonymous recursive function $func = function ($limit = NULL) use (&$func) { static $current = 10; // if condition to check value of $current. if ($current <= 0) { //break the recursion return FALSE; } // Print value of $current. echo "$current\n"; $current--; $func(); }; // Function call $func(); ?> Output: 10 9 8 7 6 5 4 3 2 1 Program 2: php <?php // PHP program to illustrate the // Anonymous recursive function $factorial = function( $num ) use ( &$factorial ) { // Base condition of recursion if( $num == 1 ) return 1; // return statement when $m is not equals to 1. return $factorial( $num - 1 ) * $num; }; // Function call print $factorial( 6 ); ?> Output: 720 Comment More infoAdvertise with us Next Article Function overloading and Overriding in PHP A Amaninder.Singh Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads PHP trait_exists() Function PHP implements a way to reuse code called Traits. The trait_exists() function in PHP is an inbuilt function that is used to check whether a trait exists or not. This function accepts the traitname and autoload as parameters and returns true if trait exists, false if not, null in case of an error. Sy 2 min read PHP | Check if a variable is a function To determine whether a passed argument is a function or not, Few of the most preferred methods are shown below. Using is_callable() Function: It is an inbuilt function in PHP which is used to verify the contents of a variable in called as a function. It can check that a simple variable contains the 3 min read Function overloading and Overriding in PHP Function overloading and overriding is the OOPs feature in PHP. In function overloading, more than one function can have same method signature but different number of arguments. But in case of function overriding, more than one functions will have same method signature and number of arguments. Funct 3 min read How to Pass an Array into a Function in PHP ? This article will show you how to pass an array to function in PHP. When working with arrays, it is essential to understand how to pass them into functions for effective code organization and re-usability. This article explores various approaches to pass arrays into PHP functions, covering different 2 min read How to get the function name inside a function in PHP ? To get the function name inside the PHP function we need to use Magic constants(__FUNCTION__). Magic constants: Magic constants are the predefined constants in PHP which is used on the basis of their use. These constants are starts and end with a double underscore (__). These constants are created b 1 min read PHP Callback Functions In PHP, the callback functions are related to the dynamic behavior and flexibility in code execution. They are used to pass custom logic or functions as arguments to other functions, letting developers change how a function behaves without changing its main structure. This makes the code more reusab 5 min read Like