1

I am looking for a way to test if a variable is an anonymous function/callback. Using the PHP.net example:

$greet = function($name)
{
    printf("Hello %s\r\n", $name);
};

You could call it with:

$greet('hi');

But what if $greet could be some other variable type and you only want to execute the callback if it's set to a function? Something like:

is_func($greet);
is_callback($greet);
is_callable($greet);

Am I missing something in the documentation?

1 Answer 1

3

is_callable($greet) will do that.

Also:

($greet instanceof Closure) might work.

Sign up to request clarification or add additional context in comments.

1 Comment

ha, dammit. of course, I use it in my own example. did not see it linked in the docs for anon functions. must've missed it. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.