Monday 6 May 2019

Difference between a lambda function and a closure (in PHP)

Difference between a lambda function and a closure (in PHP)

Question: Difference between a lambda function and a closure (in PHP)?
Lambda function is a function that assigned to a variable or passed to another function as parameter.

Example:
$greeting = function () {
  return "Hello world";
}
echo $greeting();//Hello world



A closure is a lambda function, but a lambda function is not a closure unless you specify the use keyword.

Example:

$user = "Rony";
// Create a Closure
$greeting = function() use ($user) {
  echo "Hello $user";
};
// Greet the user
$greeting();//Hello Rony