PHP TutorialCompile PHP ExtensionsContributing to the PHP CoreContributing to the PHP ManualCreate PDF files in PHPInstalling a PHP environment on WindowsPHP Alternative Syntax for Control StructuresPHP APCuPHP Array iterationPHP ArraysPHP Asynchronous programmingPHP Autoloading PrimerPHP BC Math (Binary Calculator)PHP Built in serverPHP CachePHP Classes and ObjectsPHP ClosurePHP Coding ConventionsPHP Command Line Interface (CLI)PHP CommentsPHP Common ErrorsPHP Compilation of Errors and WarningsPHP Composer Dependency ManagerPHP ConstantsPHP Control StructuresPHP CookiesPHP CryptographyPHP DateTime ClassPHP DebuggingPHP Dependency InjectionPHP Design PatternsPHP Docker deploymentPHP Exception Handling and Error ReportingPHP Executing Upon an ArrayPHP File handlingPHP Filters & Filter FunctionsPHP Functional ProgrammingPHP FunctionsPHP GeneratorsPHP Headers ManipulationPHP How to break down an URLPHP How to Detect Client IP AddressPHP HTTP AuthenticationPHP Image Processing with GDPHP ImagickPHP IMAPPHP Installing on Linux/Unix EnvironmentsPHP JSONPHP LocalizationPHP LoopsPHP Machine learningPHP Magic ConstantsPHP Magic MethodsPHP Manipulating an ArrayPHP mongo-phpPHP Multi Threading ExtensionPHP MultiprocessingPHP MySQLiPHP MySQLi affected rows returns 0 when it should return a positive integerPHP NamespacesPHP Object SerializationPHP OperatorsPHP Output BufferingPHP Outputting the Value of a VariablePHP Parsing HTMLPHP Password Hashing FunctionsPHP PDOPHP PerformancePHP PHPDocPHP Processing Multiple Arrays TogetherPHP PSRPHP Reading Request DataPHP RecipesPHP ReferencesPHP ReflectionPHP Regular Expressions (regexp/PCRE)PHP Secure Remeber MePHP SecurityPHP Sending EmailPHP SerializationPHP SessionsPHP SimpleXMLPHP SOAP ClientPHP SOAP ServerPHP SocketsPHP SPL data structuresPHP SQLite3PHP StreamsPHP String formattingPHP String Parsing

PHP Functional Programming

From WikiOD

PHP's functional programming relies on functions. Functions in PHP provide organized, reusable code to perform a set of actions. Functions simplify the coding process, prevent redundant logic, and make code easier to follow. This topic describes the declaration and utilization of functions, arguments, parameters, return statements and scope in PHP.

Closures[edit | edit source]

A closure is an anonymous function that can't access outside scope.

When defining an anonymous function as such, you're creating a "namespace" for that function. It currently only has access to that namespace.

$externalVariable = "Hello";
$secondExternalVariable = "Foo"; 
$myFunction = function() { 

  var_dump($externalVariable, $secondExternalVariable); // returns two error notice, since the variables aren´t defined 


It doesn't have access to any external variables. To grant this permission for this namespace to access external variables, you need to introduce it via closures (use()).

$myFunction = function() use($externalVariable, $secondExternalVariable) {
   var_dump($externalVariable, $secondExternalVariable); // Hello Foo

This is heavily attributed to PHP's tight variable scoping - If a variable isn't defined within the scope, or isn't brought in with global then it does not exist.

Also note:

Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing.

The parent scope of a closure is the function in which the closure was declared (not necessarily the function it was called from).

Taken from the PHP Documentation for Anonymous Functions

In PHP, closures use an early-binding approach. This means that variables passed to the closure's namespace using use keyword will have the same values when the closure was defined.

To change this behavior you should pass the variable by-reference.

$rate = .05;

// Exports variable to closure's scope
$calculateTax = function ($value) use ($rate) {
    return $value * $rate;

$rate = .1; 

print $calculateTax(100); // 5
$rate = .05;

// Exports variable to closure's scope
$calculateTax = function ($value) use (&$rate) { // notice the & before $rate
    return $value * $rate;

$rate = .1;

print $calculateTax(100); // 10

Default arguments are not implicitly required when defining anonymous functions with/without closures.

$message = 'Im yelling at you';

$yell = function() use($message) {
    echo strtoupper($message);

$yell(); // returns: IM YELLING AT YOU

Assignment to variables[edit | edit source]

Anonymous functions can be assigned to variables for use as parameters where a callback is expected:

$uppercase = function($data) {
    return strtoupper($data);

$mixedCase = ["Hello", "World"];
$uppercased = array_map($uppercase, $mixedCase);

These variables can also be used as standalone function calls:

echo $uppercase("Hello world!"); // HELLO WORLD!

Objects as a function[edit | edit source]

class SomeClass {
    public function __invoke($param1, $param2) {
        // put your code here

$instance = new SomeClass();
$instance('First', 'Second'); // call the __invoke() method

An object with an __invoke method can be used exactly as any other function.

The __invoke method will have access to all properties of the object and will be able to call any methods.

Using outside variables[edit | edit source]

The use construct is used to import variables into the anonymous function's scope:

$divisor = 2332;
$myfunction = function($number) use ($divisor) {
    return $number / $divisor;

echo $myfunction(81620); //Outputs 35

Variables can also be imported by reference:

$collection = [];

$additem = function($item) use (&$collection) {
    $collection[] = $item;


//$collection is now [1,2]

Using built-in functions as callbacks[edit | edit source]

In functions taking callable as an argument, you can also put a string with PHP built-in function. It's common to use trim as array_map parameter to remove leading and trailing whitespace from all strings in the array.

$arr = ['   one  ', 'two   ', '   three'];
var_dump(array_map('trim', $arr));

// array(3) {
//   [0] =>
//   string(3) "one"
//   [1] =>
//   string(3) "two"
//   [2] =>
//   string(5) "three"
// }

Anonymous function[edit | edit source]

An anonymous function is just a function that doesn't have a name.

// Anonymous function
function() {
    return "Hello World!";

In PHP, an anonymous function is treated like an expression and for this reason, it should be ended with a semicolon ;.

An anonymous function should be assigned to a variable.

// Anonymous function assigned to a variable
$sayHello = function($name) {
    return "Hello $name!";

print $sayHello('John'); // Hello John

Or it should be passed as parameter of another function.

$users = [
    ['name' => 'Alice', 'age' => 20], 
    ['name' => 'Bobby', 'age' => 22], 
    ['name' => 'Carol', 'age' => 17]

// Map function applying anonymous function
$userName = array_map(function($user) {
    return $user['name'];
}, $users);

print_r($usersName); // ['Alice', 'Bobby', 'Carol']

Or even been returned from another function.

Self*executing anonymous functions:

// For PHP 7.x
(function () {
    echo "Hello world!";

// For PHP 5.x
call_user_func(function () {
    echo "Hello world!";

Passing an argument into self-executing anonymous functions:

// For PHP 7.x
(function ($name) {
    echo "Hello $name!";

// For PHP 5.x
call_user_func(function ($name) {
    echo "Hello $name!";
}, 'John');

Pure functions[edit | edit source]

A pure function is a function that, given the same input, will always return the same output and are side-effect free.

// This is a pure function
function add($a, $b) {
    return $a + $b;

Some side-effects are changing the filesystem, interacting with databases, printing to the screen.

// This is an impure function
function add($a, $b) {
    echo "Adding...";
    return $a + $b;

Common functional methods in PHP[edit | edit source]

Mapping[edit | edit source]

Applying a function to all elements of an array :

array_map('strtoupper', $array);

Be aware that this is the only method of the list where the callback comes first.

Reducing (or folding)[edit | edit source]

Reducing an array to a single value :

$sum = array_reduce($numbers, function ($carry, $number) {
   return $carry + $number;

Filtering[edit | edit source]

Returns only the array items for which the callback returns true :

$onlyEven = array_filter($numbers, function ($number) {
    return ($number % 2) === 0;

Scope[edit | edit source]

In PHP, an anonymous function has its own scope like any other PHP function.

In JavaScript, an anonymous function can access a variable in outside scope. But in PHP, this is not permitted.

$name = 'John';

// Anonymous function trying access outside scope
$sayHello = function() {
    return "Hello $name!";

print $sayHello('John'); // Hello !
// With notices active, there is also an Undefined variable $name notice

Passing a callback function as a parameter[edit | edit source]

There are several PHP functions that accept user-defined callback functions as a parameter, such as: call_user_func(), usort() and array_map().

Depending on where the user-defined callback function was defined there are different ways to pass them:

Procedural style:[edit | edit source]

function square($number)
    return $number * $number;

$initial_array = [1, 2, 3, 4, 5];
$final_array = array_map('square', $initial_array);
var_dump($final_array); // prints the new array with 1, 4, 9, 16, 25

Object Oriented style:[edit | edit source]

class SquareHolder
    function square($number)
        return $number * $number;

$squaredHolder = new SquareHolder();
$initial_array = [1, 2, 3, 4, 5];
$final_array = array_map([$squaredHolder, 'square'], $initial_array);

var_dump($final_array); // prints the new array with 1, 4, 9, 16, 25

Object Oriented style using a static method:[edit | edit source]

class StaticSquareHolder
    public static function square($number)
        return $number * $number;

$initial_array = [1, 2, 3, 4, 5];
$final_array = array_map(['StaticSquareHolder', 'square'], $initial_array);
// or:
$final_array = array_map('StaticSquareHolder::square', $initial_array); // for PHP >= 5.2.3

var_dump($final_array); // prints the new array with 1, 4, 9, 16, 25