codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

16 / 56 topics
14Functions in PHP15User-Defined Functions16Anonymous Functions17Arrow Functions18Recursion in PHP
Tutorials/PHP/Anonymous Functions
🐘PHP

Anonymous Functions

Updated 2026-05-15
10 min read

Anonymous Functions

Introduction

In PHP, an anonymous function is a function that does not have a name. These functions are also known as closures because they can capture variables from their enclosing scope. Anonymous functions provide flexibility and can be used in various scenarios where named functions might be overkill or unnecessary.

Anonymous functions are particularly useful for short-lived tasks, callbacks, and when you want to define a function inline without giving it a formal name. In this tutorial, we will explore how to create and use anonymous functions in PHP.

Concept

An anonymous function is defined using the function keyword followed by its parameters (if any) and then the body of the function enclosed in curly braces {}. Unlike named functions, anonymous functions are not stored in a variable or assigned to a class method. Instead, they can be assigned to variables, passed as arguments to other functions, or returned from functions.

Here is the basic syntax for an anonymous function:

$functionVariable = function($parameter1, $parameter2) {
    // Function body
    return $parameter1 + $parameter2;
};

In this example, `$functionVariable` holds a reference to the anonymous function. You can call this function using the variable name followed by parentheses `()`.

## Examples

### Basic Usage

Let's start with a simple example of an anonymous function that adds two numbers:

```php
$add = function($a, $b) {
    return $a + $b;
};

$result = $add(5, 3);
echo $result; // Outputs: 8

In this example, the anonymous function is assigned to the variable $add. We then call this function by passing two arguments 5 and 3, and store the result in the variable $result.

Using Anonymous Functions as Callbacks

Anonymous functions are often used as callbacks. For instance, you can pass an anonymous function to PHP's built-in array functions like array_map() or array_filter().

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

$squaredNumbers = array_map(function($number) {
    return $number * $number;
}, $numbers);

print_r($squaredNumbers); // Outputs: Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25 )

In this example, array_map() applies the anonymous function to each element of the $numbers array, squaring each number.

Capturing Variables from the Enclosing Scope

One of the powerful features of anonymous functions is their ability to capture variables from their enclosing scope. This is known as closure behavior.

$message = "Hello, World!";

$greet = function() use ($message) {
    echo $message;
};

$greet(); // Outputs: Hello, World!

In this example, the anonymous function $greet captures the variable $message from its enclosing scope. The use keyword is used to specify which variables should be captured.

Returning Anonymous Functions

Anonymous functions can also be returned from other functions. This allows for more complex and dynamic behavior.

function createMultiplier($factor) {
    return function($number) use ($factor) {
        return $number * $factor;
    };
}

$double = createMultiplier(2);
echo $double(5); // Outputs: 10

$triple = createMultiplier(3);
echo $triple(4); // Outputs: 12

In this example, the createMultiplier function returns an anonymous function that multiplies a given number by the specified factor. This allows us to create different multiplier functions dynamically.

What's Next?

Now that you have learned about anonymous functions in PHP, you might want to explore another important concept: Arrow Functions. Arrow functions provide a more concise syntax for creating closures and are available from PHP 7.4 onwards. They are particularly useful for short and simple functions.

Stay tuned for the next tutorial where we will dive into arrow functions and how they can make your code cleaner and more readable!


PreviousUser-Defined FunctionsNext Arrow Functions

Recommended Gear

User-Defined FunctionsArrow Functions