Higher-Order Functions in PHP With Example

Mohasin Hossain
3 min readJan 6, 2024

--

In this post, we’ll delve into what higher-order functions are, why they matter, and provide practical examples to illustrate their power.

Understanding Higher-Order Functions:

Understanding Higher-Order Functions: A higher-order function is a concept borrowed from functional programming, where functions are treated as first-class citizens. In PHP, this means functions can be assigned to variables, passed as arguments to other functions, and returned as values from other functions.

Why Higher-Order Functions Matter:

  1. Code Reusability: Higher-order functions allow for the creation of more generic and reusable code. This can lead to cleaner and more maintainable programs.
  2. Abstraction: They enable a higher level of abstraction, making it easier to express complex operations concisely and clearly.
  3. Functional Programming Paradigm: Embracing higher-order functions aligns with the functional programming paradigm, which promotes immutability and pure functions. This can lead to more predictable and bug-resistant code.

Examples of Higher-Order Functions in PHP:

Example 1: array_map

// Using array_map to apply a function to each element of an array
$numbers = [1, 2, 3, 4, 5];
$squaredNumbers = array_map(function($number) {
return $number * $number;
}, $numbers);

// $squaredNumbers will be [1, 4, 9, 16, 25]

Example 2: array_filter

// Using array_filter to filter out even numbers from an array
$numbers = [1, 2, 3, 4, 5];
$filteredNumbers = array_filter($numbers, function($number) {
return $number % 2 !== 0;
});

// $filteredNumbers will be [1, 3, 5]

Example 3: Custom Higher-Order Function

// Creating a custom higher-order function to perform a specific operation
function applyOperation($value, $operation) {
return $operation($value);
}

// Usage
$double = function($x) { return $x * 2; };
$result = applyOperation(5, $double); // $result will be 10

Let’s see an example of code with higher order and without higher order functions.

Without higher-order function:

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

// Traditional approach without higher-order function
$evenNumbers = [];
foreach ($numbers as $number) {
if ($number % 2 === 0) {
$evenNumbers[] = $number;
}
}

// $evenNumbers will be [2, 4]

With higher-order function

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

// Using array_filter higher-order function
$isEven = function($number) {
return $number % 2 === 0;
};

$evenNumbers = array_filter($numbers, $isEven);

// $evenNumbers will be [2, 4]With higher-order function

Higher-order functions open up a world of possibilities in PHP programming. By incorporating them into your code, you can write more flexible, modular, and expressive programs. As you continue your journey in PHP development, mastering higher-order functions will undoubtedly enhance your coding skills and contribute to the creation of robust and efficient applications.

Hope you enjoyed!

Bonus: In next post, I will write about some hidden features about higher order function in Laravel Collection. So don’t forget to follow :)

Wanna learn something more:

Hope you may love my most read article : How To Use Single Responsibility Principle in PHP/Laravel
And most GitHub star repository about SOLID Principles:
https://github.com/mohasinhossain/SOLID-Principles-in-PHP

--

--

Mohasin Hossain

Senior Software Engineer | Mentor @ADPList | Backend focused | PHP, JavaScript, Laravel, Vue.js, Nuxt.js, MySQL, TDD, CI/CD, Docker, Linux