Skip to content
Advertisement

Recursive IIFE(Immediatly invoked function) in php 7

is it possible to use recursion with immediatly invoked function with php 7?
For exemple if i want to code a recursive version of a fibonacci series?

Following exemple does not work, but i hope it will help you to get my idea.

echo (function fn($x) {
     if($x==1||$x==0?0){
        return $x;
     }else{
        return fn($x-1) + fn($x+1);
     }
})(4);

Advertisement

Answer

Yes you can use iife in php 7 like this:

(function() { echo "yes, this works in PHP 7.n"; })();

or

$arr = array();
($recursive = function (&$argument)
{
    global $recursive;

    if (count($argument) < 10)
    {
        $argument[] = 'foo';
        $recursive($argument);
    }
})($arr);
print_r($arr);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement