Current Location: Home> Latest Articles> PHP Arrow Functions Tutorial: Easily Implement Functional Programming Style

PHP Arrow Functions Tutorial: Easily Implement Functional Programming Style

M66 2025-09-19

Introduction

Functional programming is a programming paradigm that emphasizes building program logic using pure functions. In traditional object-oriented programming, we typically organize code with classes and objects, while functional programming focuses on defining and invoking functions. PHP 7.4 introduced arrow functions, offering a more concise syntax for anonymous functions, simplifying functional programming practices. This article explains the basics of arrow functions and demonstrates their use with practical examples.

What Are Arrow Functions

Arrow functions were originally introduced in JavaScript ES6 and later added to PHP 7.4 to simplify the definition of anonymous functions. Arrow functions automatically capture variables from the outer scope and omit the function and return keywords. The basic syntax is:

(parameters) => expression;

Defining and Using Arrow Functions

Here is a simple example that defines an arrow function to calculate the square of a number:

$square = fn($num) => $num ** 2;
echo $square(4); // Output: 16

In this example, the arrow function fn($num) => $num ** 2 takes a parameter $num and returns its square. By calling $square(4), we get the result 16.

Arrow functions can accept one or multiple parameters. When there are multiple parameters, they should be enclosed in parentheses, for example: (param1, param2) => expression.

Arrow Functions in Functional Programming

Functional programming emphasizes treating functions as first-class citizens, meaning functions can be passed as arguments or returned from other functions. Arrow functions simplify function definition and passing, making functional programming more concise and efficient.

Here is an example that uses an arrow function to implement a higher-order map function, applying a transformation to each element of an array:

$numbers = [1, 2, 3, 4, 5];
$double = fn($num) => $num * 2;

function map(array $array, callable $transform): array
{
    return array_map($transform, $array);
}

$result = map($numbers, $double);
print_r($result); // Output: [2, 4, 6, 8, 10]

In this example, the arrow function $double multiplies each input by 2. The higher-order function map takes an array and a function, applies the function to each element using array_map, and returns the resulting array. The final output array has each element doubled.

This example illustrates the conciseness and flexibility of arrow functions in functional programming, allowing functions to be defined and passed quickly.

Conclusion

Arrow functions, introduced in PHP 7.4, provide a concise way to define anonymous functions and automatically capture variables from the outer scope. They make it easier to adopt a functional programming style in PHP. This article covered arrow function syntax, usage, and practical examples for array manipulation and higher-order functions, helping improve the readability and simplicity of PHP code.

References

PHP Official Documentation: Arrow Functions https://www.php.net/manual/en/functions.arrow.php