Anonymous function or Lambda function

An anonymous function is simply a function with no name.

A Lambda is an anonymous function that can be assigned to a variable or passed to another function as an argument. If you are familiar with other programming languages like Javascript etc., you will be very familiar with anonymous functions.

// Regular function
function dosomething () {
return “Hello there”;

Function can be called something like:

echo dosomething();
// Returns “Hello there”

An anonymous function has no name so you would define it like this:

// Anonymous function
function () {
return “Hello there”;

How to use Ananymous functions –

Because the function has no name, you can’t call it like a regular function.

// assiging to variable, Anonymous/lamda function

$dosomething = function () {
return “Hello there”;

// Calling Ananymous function
echo $dosomething();
// Returns “Hello there”

To use the anonymous function, we need to assign it to a variable and then call that variable as a function.

You could also pass the function to another function, like this:

// Pass Ananymous\Lambda to another regular function
function anotherfunction ($dosomething){
echo $dosomething();

// Calling function
return “Hello there”;

3 Responses to Anonymous function or Lambda function

  1. Digvijay Tiwari says:

    Can I write Lambda function using PHP?

  2. تسديد قروض says:

    It’s appropriate time to make some plans for the future and it is time
    to be happy. I’ve read this post and if I could I desire to suggest you some interesting things or advice.
    Maybe you could write next articles referring to this article.
    I wish to read even more things about it!

Leave a Reply

Your email address will not be published. Required fields are marked *