Functional Programming in PHP: From A to Z — Complete Guide

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

Introduction to Functional Programming in PHP



Functional programming (FP) is a paradigm in which computation is treated as the evaluation of mathematical functions, avoiding state changes and mutable data. Although PHP was originally created as an imperative language, starting with version 5.3 (2009), powerful tools for a functional style were added: anonymous functions, closures, support for higher-order functions, and much more.



In this article, we will cover all key concepts of functional programming in PHP — from basic anonymous functions to advanced techniques like currying and function composition. You will learn how to write cleaner, more predictable, and more testable code.



1. Anonymous Functions and Closures



Anonymous Functions



An anonymous function (lambda) is a function without a name. It can be assigned to a variable, passed as an argument, or returned from another function.



<?php// Simple anonymous function$square = function($n) {    return $n * $n;};

echo $square(5); // 25

// Anonymous function as a callback for array_map$numbers = [1, 2, 3, 4];$squared = array_map(function($n) { return $n * $n;}, $numbers);

print_r($squared); // [1, 4, 9, 16]?>


Closures



A closure is an anonymous function that can capture variables from the outer scope using the use keyword. This is one of the most powerful features of FP in PHP.



<?php$multiplier = 3;

$multiply = function($n) use ($multiplier) { return $n * $multiplier;};

echo $multiply(10); // 30

// Mutable variable (passed by reference)$counter = 0;$increment = function() use (&$counter) { $counter++;};

$increment();$increment();echo $counter; // 2?>


2. Pure Functions and Data Immutability



Pure Functions



A pure function is a function that:

  • Always returns the same result for the same arguments (deterministic)
  • Has no side effects (does not modify external variables, does not output data, does not work with databases)


<?php// NOT a pure function (depends on a global variable)$taxRate = 0.2;function calculatePriceDirty($price) {    global $taxRate;    return $price * (1 + $taxRate);}

// Pure functionfunction calculatePricePure($price, $taxRate) { return $price * (1 + $taxRate);}

echo calculatePricePure(100, 0.2); // 120 — always predictable?>


Immutability



In FP, it is common practice not to modify existing data but to create new data. In PHP, you can use immutable objects or simply return new arrays.



<?php// Bad: modifying the original array$numbers = [1, 2, 3];array_push($numbers, 4); // mutation

// Good: creating a new array$numbers = [1, 2, 3];$newNumbers = array_merge($numbers, [4]);

// Function returning a new arrayfunction addItem(array $arr, $item): array { $result = $arr; $result[] = $item; return $result;}

$original = [1, 2];$updated = addItem($original, 3);print_r($original); // [1, 2] — original unchangedprint_r($updated); // [1, 2, 3]?>


3. Higher-Order Functions: array_map, array_filter, array_reduce



Higher-order functions are functions that take other functions as arguments or return them. PHP has three built-in key functions for working with arrays in a functional style.



array_map — Transforming Elements



<?php$users = [    ['name' => 'Alice', 'age' => 25],    ['name' => 'Bob', 'age' => 30],];

// Extract only names$names = array_map(function($user) { return $user['name'];}, $users);

print_r(

Blogs

Book Recommendations