Functional Programming in PHP: From Basics to Practice

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Functional Programming in PHP: A Modern Approach to Writing Code

PHP is traditionally associated with imperative and object-oriented programming styles. However, with the release of versions 7.4, 8.0, and 8.1, the language has gained powerful tools for functional programming (FP). This paradigmatic approach allows you to write more predictable, testable, and concise code, especially when working with arrays, collections, and asynchronous tasks. In this article, we will break down the key concepts of FP in PHP and show how to apply them in practice.



1. Fundamentals of Functional Programming in PHP

Functional programming is built on several fundamental principles:

  • Pure functions — the result depends only on arguments, with no side effects.
  • Immutability — data is not changed, but new copies are created.
  • Higher-order functions — functions that take other functions as arguments or return them.
  • Recursion — replacing loops with self-calling functions.

PHP supports these concepts thanks to anonymous functions (closures), arrow functions (since PHP 7.4), and built-in array functions.



// Example of a pure functionfunction add(int $a, int $b): int {    return $a + $b;}

// Example of an anonymous function (closure)$multiply = function(int $x, int $y): int { return $x * $y;};

echo $multiply(3, 4); // 12

Important: Pure functions in PHP should not modify global variables or perform input/output (e.g., echo). This ensures predictability and simplifies debugging.



2. Anonymous Functions, Closures, and Arrow Functions

Anonymous functions (closures) are functions without a name that can be passed as arguments or assigned to variables. They can capture variables from the outer scope using the use keyword.



$factor = 2;$double = function(int $value) use ($factor): int {    return $value * $factor;};

echo $double(5); // 10

Since PHP 7.4, arrow functions have appeared — a more concise syntax for single-line closures. They automatically capture variables from the scope.



$factor = 2;$double = fn(int $value): int => $value * $factor;

echo $double(5); // 10

When to use: Arrow functions are ideal for simple transformations (e.g., in array_map), while classic closures are better for complex logic with multiple statements.



3. Higher-Order Functions: map, filter, reduce

In PHP, built-in functions are used for working with arrays in a functional style:

  • array_map() — applies a function to each element of an array.
  • array_filter() — filters an array based on a condition.
  • array_reduce() — reduces an array to a single value.


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

// map: double each element$doubled = array_map(fn($n) => $n * 2, $numbers);print_r($doubled); // [2, 4, 6, 8, 10, 12]

// filter: keep only even numbers$even = array_filter($numbers, fn($n) => $n % 2 === 0);print_r(array_values($even)); // [2, 4, 6]

// reduce: sum of all elements$sum = array_reduce($numbers, fn($carry, $n) => $carry + $n, 0);echo $sum; // 21

Tip: Use array_values() after array_filter() to reset the array keys, otherwise they may be non-sequential.



These functions allow you to replace foreach loops with declarative call chains, making the code more readable and less error-prone.

Blogs

Book Recommendations