PHP Anonymous Functions

Serghei Pogor
3 min readMay 2, 2023

Anonymous functions, also known as lambda functions, are a type of function in PHP that do not have a name and can be defined inline, allowing you to create functions that are used once and then discarded. They are created using the function keyword followed by a set of parentheses that contain any parameters, and then a curly brace block that contains the function's code.

One of the most common use cases for anonymous functions is as callback functions. For example, if you want to sort an array using a custom sorting algorithm, you can use usort() and pass in an anonymous function as the second parameter. Here's an example:

$myArray = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];

usort($myArray, function($a, $b) {
if ($a == $b) {
return 0;
} else {
return ($a < $b) ? -1 : 1;
}
});

print_r($myArray);

In this example, the anonymous function is used to compare two values $a and $b. If they are equal, it returns 0. If $a is less than $b, it returns -1. Otherwise, it returns 1. This custom sorting algorithm sorts the array in ascending order.

Anonymous functions can be a bit intimidating at first, but once you get the hang of them, they’re incredibly useful.

  • Anonymous functions are functions that do not have a name and are defined inline within other code.
  • They are created using the “function” keyword, followed by the function parameters in parentheses, then the function body within curly braces.
  • Anonymous functions can be stored in variables or passed as arguments to other functions.
  • They are useful for creating functions on the fly, especially when the logic is only used in one place in code.
  • Anonymous functions can contain multiple statements and more complex logic than arrow functions.

Pros of anonymous functions:

  • They are more flexible than arrow functions and can contain more complex logic.
  • They can be stored in variables or passed as arguments to other functions, making them very versatile.

Cons of anonymous functions:

  • They can be more difficult to read and understand than arrow functions, especially when they are nested within other code.
  • They can be overused, leading to code that is difficult to maintain and understand.

Here are a few different ways to implement anonymous functions:

  1. Assigning an anonymous function to a variable:
$add = function($a, $b) {
return $a + $b;
};

$result = $add(2, 3); // $result equals 5

2. Passing an anonymous function as an argument to another function:

$numbers = [1, 2, 3, 4, 5];

$filtered = array_filter($numbers, function($number) {
return $number % 2 == 0;
});
// $filtered equals [2, 4]

3. Defining an anonymous function within a class method:

class Calculator {
public function calculate($a, $b, $operation) {
$operations = [
'add' => function($a, $b) {
return $a + $b;
},
'subtract' => function($a, $b) {
return $a - $b;
},
'multiply' => function($

In conclusion, anonymous functions are a powerful tool in PHP that can make your code more concise and easier to read in certain situations. They are particularly useful for callback functions and closures. However, you should use them judiciously, as they can make your code harder to read if they are too complex or used in too many places.

--

--

Serghei Pogor
Serghei Pogor

Written by Serghei Pogor

Good code is its own best documentation

No responses yet