What is the use of lambda functions in PHP?

The lambda function is used to first store data into a variable and then to pass it as arguments for the usage in other methods or functions.

example:

<?php

$input = array(2, 5, 10);

$output = array_filter($input, function ($x) { return $x > 2; });

?>

Lambda function definition here:

<?php

function ($x) { return $x > 2; });

?>

This is used further to store data into a variable, and then you can use it when required without the requirement of defining it again.

Sign In or Register to comment.