Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials PHP Anonymous Functions & Closures
PHP Intermediate FREE

Anonymous Functions & Closures

Lesson 2 of 17 Intermediate Interactive

Anonymous functions (also called closures) are functions without a name. They are assigned to variables and are especially useful for short callbacks.

Anonymous Functions

Assign a function keyword directly to a variable — no function name needed.

Arrow Functions (PHP 7.4+)

A shorter syntax for simple single-expression functions: fn($x) => $x * 2

Closures with use

Anonymous functions can access variables from the parent scope using the use keyword.

Common Use Cases

  • Callbacks for array_map, array_filter, array_reduce
  • Event handlers
  • Sorting with custom logic

Syntax

PHP
<?php
// Anonymous function

$square = function($x) {
    return $x * $x;
};

// Arrow function

$double = fn($x) => $x * 2;

// Closure with use keyword

$greeting = "Hello";
$sayHello = function($name) use ($greeting) {
    return "$greeting, $name!";
};

echo $square(5);    // 25

echo $double(4);    // 8

echo $sayHello("Ali"); // "Hello, Ali!"

?>
PHP Closures
PHP
<?php
// Basic closure
$greet = function ($name) {
    return "Hello, $name!";
};
echo $greet("Alice") . "
";

// Closure capturing variables
$prefix = "User: ";
$format = function ($name) use ($prefix) {
    return $prefix . $name;
};
echo $format("Bob") . "
";

// Arrow functions (PHP 7.4+)
$double = fn($n) => $n * 2;
echo "Double: " . $double(21) . "
";

// Passing closures
function apply($value, $fn) {
    return $fn($value);
}
echo apply(5, fn($x) => $x * $x) . "
";
?>

Practice

1
Exercise

Use array_map with an arrow function to convert an array of strings to uppercase.

Answer
<?php
$words = ["hello", "world", "php"];
$upper = array_map(fn($w) => strtoupper($w), $words);
echo implode(", ", $upper);
?>
2
Exercise

Write a closure that uses the use keyword to access a $taxRate variable from the parent scope.

Answer
<?php
$taxRate = 0.10;
$applyTax = function($price) use ($taxRate) {
    return $price + ($price * $taxRate);
};
echo $applyTax(100); // 110
?>

Quick Quiz

1

What is an arrow function in PHP?

Arrow functions (fn) are a concise syntax for simple functions that return a single expression.

2

What keyword allows anonymous functions to access parent scope variables?

The use keyword imports variables from the parent scope into the anonymous function.

3

Which PHP version introduced arrow functions?

Arrow functions (fn syntax) were introduced in PHP 7.4.

Interview Questions

An anonymous function uses the full function() syntax and requires a return keyword. An arrow function (fn()) is a concise shorthand — it implicitly returns the expression and automatically captures variables from the parent scope without needing use. Arrow functions are best for short, single-expression callbacks.

Closures are used as callbacks in array operations (map, filter, reduce), sorting functions (usort, uasort), event systems, middleware in Laravel, and any place where you need a small, disposable function. They keep code concise without polluting the namespace with named functions.