Anonymous Functions

Last updated

What is Anonymous Functions?

Anonymous functions in PHP, also known as closures, are functions that do not have a specified name. They are most frequently used as a value for callback parameters, but can be used in many other ways. When creating an anonymous function, you can also inherit variables from the parent scope. Here's a basic usage example:

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

$greet('World');
$greet('PHP');

In this example, we're creating an anonymous function and assigning it to the variable $greet. We then call this anonymous function using $greet with 'World' and 'PHP' as arguments.

Free resources to learn Anonymous Functions

More in stage 5: Functions

Anonymous Functions: common questions

Can I learn Anonymous Functions in Hindi?

This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain Anonymous Functions in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.

Is this Anonymous Functions tutorial for beginners?

It is lesson 34 of 120 in the free PHP roadmap, in stage 5, Functions. If you are new to PHP, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.

Still confused? Ask me