Lambda Functions in PHP

Sheeraz Gul Feb 27, 2022
  1. Demonstrate How to Pass a Lambda Function as a Parameter in PHP
  2. Demonstrate How to Store a Lambda Function in a Variable in PHP
  3. Difference Between Lambda and Closure Functions in PHP
Lambda Functions in PHP

Lambda functions in PHP are anonymous functions that can be stored in a variable or passed as an argument to other functions.

A closure is a type of lambda function that should be aware of its surroundings, but not every lambda is a closure.

Lambda requires a temporary function that will be used once.

Demonstrate How to Pass a Lambda Function as a Parameter in PHP

<?php
$demo_array=array(4,3,1,2,8,9,3,10,5,13);
//usort is a built-in PHP function to sort an array with a given condition.
usort($demo_array, function ($a, $b) {
    return ($a<$b)?-1:1;
});
// We passed an anonymous function that sorts the array in ascending order; this function is the lambda function.

foreach($demo_array as $val){
    echo $val;
    echo "<br>";
}
?>

usort() is a built-in PHP function that takes an array and a function as a parameter; the function should contain the condition on which you want to sort the array.

The code above tries to sort the array in ascending order with the help of the given lambda function.

Output:

1
2
3
3
4
5
8
9
10
13

Demonstrate How to Store a Lambda Function in a Variable in PHP

The other functionality of the lambda function in PHP can be stored in variables.

<?php
$divide = function ($a, $b) {
    return $a / $b;
};
echo $divide(10, 2);
echo "<br>";
echo $divide(15, 2);
echo "<br>";
echo $divide(10, 3);
echo "<br>";
?>

The code above creates a lambda function that can divide two numbers and store them as variables. Now it can be used anywhere in the vicinity as a variable.

Output:

5
7.5
3.3333333333333

Difference Between Lambda and Closure Functions in PHP

Every closure function is a lambda function, but not every lambda is a closure function. A closure function needs to be aware of its surroundings.

<?php
$range = range(10, 20);
//Print the squared number from 10-20 range - an example of lambda.
echo "The output for lambda function: <br>";
print_r(array_map(function($number){
    return $number**2;
}, $range));
echo "<br><br>";

$power = 2;
//Print a warning.
print_r(array_map(function($number){
    return $number**$power;
}, $range));
echo "<br><br>";

//Print the squared number from 10-20 range - an example of closure.
echo "The output for closure function: <br>";
print_r(array_map(function($number) use($power) {
    return $number**$power;
}, $range));
?>

The second example is we try to square the number through a variable, and it can’t; that is where the closure function comes.

We use the use keyword to pass the variable to the function. Now, this lambda function is a closure function.

Output:

The output for lambda function:
Array ( [0] => 100 [1] => 121 [2] => 144 [3] => 169 [4] => 196 [5] => 225 [6] => 256 [7] => 289 [8] => 324 [9] => 361 [10] => 400 )

Notice: Undefined variable: power in C:\Apache24\htdocs\test.php on line 15
Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 [6] => 1 [7] => 1 [8] => 1 [9] => 1 [10] => 1 )

The output for closure function:
Array ( [0] => 100 [1] => 121 [2] => 144 [3] => 169 [4] => 196 [5] => 225 [6] => 256 [7] => 289 [8] => 324 [9] => 361 [10] => 400 ) 

The middle function cannot square the numbers because it cannot identify the outside variable. We pass the variable using the use keyword, and the lambda becomes a closure function.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook