Anonymous functions are useful when using functions that require a callback function like array_filter or array_map do:

$arr = range(0, 10);
$arr_even = array_filter($arr, function($val) { return arr_square = array_map(function($val) { return val; }, $arr);

Otherwise you would need to define a function that you possibly only use once:

function isEven($val) { return arr_even = array_filter($arr, 'isEven');
function square($val) { return val; }
$arr_square = array_map('square', $arr);
Answer from Gumbo on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ functions.anonymous.php
PHP: Anonymous functions - Manual
Anonymous functions may be declared statically. This prevents them from having the current class automatically bound to them. Objects may also not be bound to them at runtime. Example #6 Attempting to use $this inside a static anonymous function
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ what-is-anonymous-function-in-php
PHP Anonymous Function - GeeksforGeeks
July 23, 2025 - An anonymous function, also known as a closure in PHP, is a function that does not have a name.
Discussions

Why and how do you use anonymous functions in PHP? - Stack Overflow
Anonymous functions are available from PHP 5.3. Should I use them or avoid them? If so, how? Edited: just found some nice trick with PHP anonymous functions: $container = new More on stackoverflow.com
๐ŸŒ stackoverflow.com
php - Why use anonymous function? - Stack Overflow
Possible Duplicate: How do you use anonymous functions in PHP? Why should i use an anonymous function? I mean, what's the real deal using it? I just don't really get this. I mean, you use funct... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Why would I ever use an anonymous function stored in a variable when I can use a normal function instead?
So that you can pass it to multiple functions, eg: doStuff1( $add ) doStuff2( $add ) It's been a while since I did PHP, so maybe the following is possible anyway? function add() { // } doSuff( $add ) ... but in a lot of languages you can't. More on reddit.com
๐ŸŒ r/AskProgramming
18
11
February 14, 2024
Passing parameter to Usort?
Write a function that takes the arguments and have it return a new anonymous function. function mySortFunc(mixed $col) { return function($a, $b) use ($col) { return strnatcmp($a[$col], $b[$col]); }; } usort($myArray, mySortFunc(2)); More on reddit.com
๐ŸŒ r/PHPhelp
9
5
April 24, 2023
People also ask

How do you declare and use an anonymous function in PHP?
You declare it using the function keyword and assign it to a variable. Example:

$sum = function($a, $b) { return $a + $b; }; 
echo $sum(5, 3);
You can also pass it as a callback in array functions.
๐ŸŒ
flatcoding.com
flatcoding.com โ€บ home โ€บ php anonymous function: how it works with examples
PHP Anonymous Function: How It Works with Examples - FlatCoding
What is an anonymous function?
An anonymous function is a function defined without a name. It can be assigned to a variable or passed directly as an argument. In PHP, you can write:

$greet = function($name) { return "Hello, " . $name; }; 
echo $greet("World"); 
๐ŸŒ
flatcoding.com
flatcoding.com โ€บ home โ€บ php anonymous function: how it works with examples
PHP Anonymous Function: How It Works with Examples - FlatCoding
What is the difference between anonymous functions and arrow functions in PHP?
Arrow functions use the fn keyword and automatically capture variables from the parent scope. They are limited to a single expression. Example:

$factor = 2; 
$multiply = fn($x) => $x * $factor; 
echo $multiply(5); 
๐ŸŒ
flatcoding.com
flatcoding.com โ€บ home โ€บ php anonymous function: how it works with examples
PHP Anonymous Function: How It Works with Examples - FlatCoding
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ php โ€บ php_anonymous_functions.htm
PHP - Anonymous Functions
Normally, when we define a function in PHP, we usually provide it a name which is used to call the function whenever required. In contrast, an anonymous function is a function that doesnt have any name specified at the time of definition.
๐ŸŒ
FlatCoding
flatcoding.com โ€บ home โ€บ php anonymous function: how it works with examples
PHP Anonymous Function: How It Works with Examples - FlatCoding
September 21, 2025 - You run the function by calling __invoke with two arguments. A closure is an anonymous function implemented with the closure mechanism that captures variables from its parent scope. It keeps these variables even when the outer function exits.
Find elsewhere
Top answer
1 of 6
91

Anonymous functions are useful when using functions that require a callback function like array_filter or array_map do:

$arr = range(0, 10);
$arr_even = array_filter($arr, function($val) { return arr_square = array_map(function($val) { return val; }, $arr);

Otherwise you would need to define a function that you possibly only use once:

function isEven($val) { return arr_even = array_filter($arr, 'isEven');
function square($val) { return val; }
$arr_square = array_map('square', $arr);
2 of 6
25

Anonymous functions are available from PHP 5.3.

Anonymous functions have been available in PHP for a long time: create_function has been around since PHP 4.0.1. However you're quite right that there is a new concept and syntax available as of PHP 5.3.

Should I use them or avoid them? If so, how?

If you've ever used create_function before, then the new syntax can simply slip right in where you used that. As other answers have mentioned, a common case is for 'throwaway' functions where they are to be used just once (or in a single place at least). Commonly that comes in the form of callbacks for the likes of array_map/reduce/filter, preg_replace_callback, usort, etc..

Example of using anonymous functions to count the number of times letters appear in words (this could be done in a number of other ways, it is just an example):

$array = array('apple', 'banana', 'cherry', 'damson');

// For each item in the array, count the letters in the word
$array = array_map(function($value){
    $letters = str_split($value);
    $counts  = array_count_values($letters);
    return $counts;
}, $array);

// Sum the counts for each letter
$array = array_reduce($array, function($reduced, $value) {
    foreach ($value as $letter => $count) {
        if ( ! isset($reduced[$letter])) {
            $reduced[$letter] = 0;
        }
        $reduced[$letter] += $count;
    }
    return $reduced;
});

// Sort counts in descending order, no anonymous function here :-)
arsort($array);

print_r($array);

Which gives (snipped for brevity):

Array
(
    [a] => 5
    [n] => 3
    [e] => 2
    ... more ...
    [y] => 1
)
๐ŸŒ
Jacobemerick
blog.jacobemerick.com โ€บ web-development โ€บ anonymous-functions-in-php
Anonymous Functions in PHP | Jacob Emerick's Blog
April 17, 2015 - A new magic method, __invoke, was added to PHP, and anonymous functions were instances of Closure that used __invoke. (__invoke gets called when you call an object as if it was a function, an awesome shorthand for simple utility-like classes). Most of that is behind the scenes.
๐ŸŒ
Envato Tuts+
code.tutsplus.com โ€บ home โ€บ coding fundamentals
Anonymous and Arrow Functions in PHP | Envato Tuts+
April 29, 2021 - This tutorial will teach you about the features and uses of anonymous functions in PHP. You'll also learn about the newer arrow function syntax in PHP.
๐ŸŒ
Medium
sergheipogor.medium.com โ€บ php-anonymous-functions-e45dc5d20019
PHP Anonymous Functions. Anonymous functions, also known asโ€ฆ | by Serghei Pogor | Medium
May 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.
๐ŸŒ
Medium
sandeeppant.medium.com โ€บ understanding-callbacks-and-anonymous-functions-in-php-laravel-3f20c430e694
Understanding Callbacks and Anonymous Functions in PHP Laravel | by Sandeeppant | Medium
February 22, 2025 - This feature allows you to define functions inline, without having to declare them globally. You can assign them to variables, pass them as arguments, or return them from other functions.
๐ŸŒ
Some drops of PHP
drops-of-php.hi-folks.dev โ€บ 04-functions โ€บ 01-fns-anonymous
Anonymous functions in PHP | Some drops of PHP
A classical declaration of function ... function you need to use the name (for example sum(5,3)). Anonymous functions are functions without a name....
๐ŸŒ
Reintech
reintech.io โ€บ blog โ€บ php-and-closures-anonymous-functions-and-variable-scoping
PHP and Closures: Anonymous Functions and Variable Scoping | Reintech media
February 1, 2026 - This pattern is used extensively ... implementing and extending these patterns. An Anonymous Function is a function that is not bound to an identifier....
๐ŸŒ
Lukasz Tkacz Blog
tkacz.pro โ€บ php-anonymous-functions-lambdas-and-closures
PHP: anonymous functions, lambdas and closures - Lukasz Tkacz Blog
November 17, 2018 - What is anonymous function in PHP? As a name suggests, itโ€™s a function without name. We can create at any time, but it may be useless.
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ curious-situations-anonymous-functions-php-alexandr-baikuzin
Curious Situations with Anonymous Functions in PHP
September 8, 2023 - Anonymous functions, also known as closures, are a powerful tool in the PHP programming language. They allow you to create functions on-the-fly without explicitly declaring a name, which can be very convenient in various scenarios.
๐ŸŒ
Codeguage
codeguage.com โ€บ v1 โ€บ courses โ€บ php โ€บ functions-closures
Mini courses - Learn in a week! | Codeguage
You shouldn't need to spend tons and tons of hours in learning from just one course ยท The advanced aspects of styling in CSS
๐ŸŒ
Altorouter
altorouter.com โ€บ home โ€บ php closures: anonymous functions & scoping
PHP Anonymous Function: A Guide
January 22, 2024 - Closures, in programming, represent functions that encapsulate and retain their surrounding context, including any variables provided to them. In PHP, these closures are realized through anonymous functions, that lack a formal name.
๐ŸŒ
Medium
zappytalks.medium.com โ€บ php-anonymous-functions-and-callbacks-236926f43fa9
PHP: Anonymous functions and callbacks | by ZappyTalks | Medium
November 4, 2019 - The key differences between both that anonymous functions have no specified name and can be assigned in a variable. These functions are most useful as callbacks. See following example to create a simple anonymous function: <?php $func = function($name){ return $name; }; echo $func('The Elementary'); ?>