Search code examples
phplambdareturnanonymous-function

Can an anonymous function return itself?


In some PHP quiz I got the following task - I have to return true on the following:

function foo($x)
{
    return $x === $x();
}

foo(__________ALLOWED_INPUT____________);

Now my idea was to pass an anonymous function which returns itself:

foo(function() { return $this_function; })

However I did not yet figure out a way to do this. Is it possible somehow?

PS: Nice Game (https://returntrue.win/?level=6).


Solution

  • You can create an anonymous function that returns a reference to itself:

    foo($x=function()use(&$x){return$x;})
    

    http://sandbox.onlinephpfunctions.com/code/743f72c298e81e70f13dc0892894911adfb1b072