Lambda, Closure and Callback in PHP

Tram Ho

  • Hello everyone, in this article, I would like to share a little of what I have learned about Lambda, Closure and callbacks in PHP, hope everyone will follow.

1 Lambda

1.1) What is Lambda?

  • Lambda  is an anonymous function that can be declared and defined anywhere and is not reusable.
  • Lambda only exists in the scope it defines, so if you use it out of scope, this function will no longer work.
  • Lambda can be assigned to a variable to use

1.2) Syntax

  • Regular function declaration in PHP:

  • To declare lambda in PHP we use the syntax:

  • Or you can use the function create_function() in PHP

  • Where argument are the parameters you want to pass in the anonymous parameter.
  • Normal functions want to perform we need to call its name, and lambda is an anonymous function, so to call it, we will assign it to a variable or pass it as a parameter

  • Above we have assigned lambda to a variable, but if we want to pass it as a parameter it will look like this:

  • Using anonymous functions or Lambda is very useful when functions we only need to use once.
  • Usually, we will need a function to do a job, but that doesn’t mean we will use it in the global scope. Instead of having a function that is used once and then no longer used, we can use an anonymous function instead.

2) Closure

2.1) What is Closure?

  • A Closure is essentially a lambda, but a closure has the added function of being able to use variables outside of the scope in which it was created.
  • One defining characteristic of a Closure function is that it will have the keyword use after the function’s name.

2.2) Syntax

-To declare closure in PHP we will use the syntax:

  • Inside:
    • argument are the parameters you want to pass in closure
    • scope is the list of variables outside closure that you want to use in closure.

For example:

  • In the above example you can see that Closure can access variables $name because it is declared in the keyword use in the definition closure. Suppose if we change the value of the variable $name in closure, it will not affect the original value of the variable $name outside, To be able to change the value of the variable $name outside, we need to pass the reference (&$name) of that variable into the keyword use

For example:

  • Closure is very useful when using PHP functions that accept a function callback like array_maparray_filterarray_walk
  • In the next example we use the function array_map to multiply elements by a pre-declared factor hệ

For example:

  • Result:

  • In the above example, we see that it is not necessary to create a function just to multiply two numbers, so we use Closure to do something like this and then never use it.
  • Closure is a very popular concept in today’s PHP frameworks, using Lambda and Closure to perform small jobs without affecting the namespace of the project and especially using it very well in callbacks.
  • Example in Laravel

3 Callback

3.1) What is callback?

  • Callback is the concept that a function is passed to another function as a parameter so that it can be executed before or after an event or a state change.

For example:

  • Above I have defined two functions sayHello and sayGoodbye then I pass the function sayGoodbye into the function sayHello as a parameter, in the function sayHello then I can code stuff, then I’ll call the $callback (is the function sayGoodbye) at the end
  • Above is a simple example of callback in PHP, function sayGoodbye passed to function sayHello as an argument.
  • The above example is quite easy to understand, but not to mention using the result of the call function in the called function, I will go through an example later.

For Example:

  • In the above example, when calling the callback function, I also pass the callback  value** $full_name** into the callback function
  • The essence of $callback(full_name) is to call a built-in PHP function called call_user_func, so callback(full_name) is equivalent to call_user_func(callback,full_name). Using callback may have an error if we callback to an undefined function, so we need to check if callback exists before using it

  • Callback Usually used when the application needs to perform another function based on the context or state, or in other words wants to do something when an event occurs..
    • Use with anonymous functions or with Closure.
    • Multi-threaded programming (multiple thread).


  • So I have finished presenting to everyone about anonymous lambda functions, closures and callbacks in PHP. Thank you everyone for following my post

Reference source

Share the news now