Computer Programming web Web programming Tips

PHP functional programming.

By Sergey Skudaev

Since PHP version 5.3, PHP support functional programming. What does it mean? We could create function in PHP before. What is the difference? The difference is that now we can assign function to a variable or pass it as a parameter to a new function.

Let's look at the following eample of PHP code that converts Fahrenheit temperature to Celsius.

// function-variable that return value
function getFr(){

return 95;


// create ananimous (Lambda) function converting F to C


//converting Fahrenheit to Celsius and
//rounding result to a whole number

return (round(($f-32)*(5/9),0));


Since the anonymous function does not have a name, to call it we have to assign it to a variable. A function that can be assigned to a variable is called Lambda function.

$C = function($f){

return (round(($f-32)*(5/9),0));


//Call function assigned to $C
echo '$C(getFr());='. $C(getFr());


OUTPUT:  $C(getFr());=35

We can modify the variable function in the following way:

function getF($t){

return $t;


// we can assign the function to a variable:
$F = getF(90);

echo $F."<br>"; // output: 32

//Again creating a lambda function:

$C = function($f){

return (round(($f-32)*(5/9),0));


//Call $C and pass $F variable.
echo $C($F);


//Call $C passing getF() function as a parameter

echo $C(getF(95));


What is a closure? Closure is a function that can access variables outside the scope in which it was created.

For example, function convert($f) from the folowing example returns an anonymous function and that anonymous function can access $f variable that exists outside the scope of that anonymous function.


// Create a Closure
function convert($f) {
              return function() use ($f) {
                  echo (round(($f-32)*(5/9),0));

$converter = convert($F);



Another closure example. An anonymous function assigned to $convertToC variable accessing $n variable declared outside the function scope.

// Create a number
$n = 32;

// Create a Closure
$convertToC = function($ft) use ($n) {
  return (($ft - $n) * 5/9);

echo $convertToC(95); //


The next example demonstrates using partial function. In PHP a partial function is originated from a function with multiple parameters.

Original function:

	$cube = function ($w, $h, $l) {

	    return $w * $h* $l;


A Partial function:

	$partial_function1 = function ($width) use ($cube) {

	    return $cube ($width, 3, 5);


echo $partial_function1(2);


In previous example a partion function took one paramatar of three. It is possible to use partial function that takes 2 or all three paramaters.

$partial_function2 = function ($w, $h) use ($cube) {

	    return $cube ($w, $h, 7);


echo $partial_function2(2, 3);


The partial function example with all three paramaters.

$partial_function3 = function ($a, $b, $c) use ($cube) {

	    return $cube ($a, $b, $c);


echo $partial_function3(2, 3, 9);


Lambda functions and closures are new for PHP, but they are widely used in JavaScript, specially in jQuery for a long time. These new PHP features offer developers even more flexibility.

My eBooks on


No comments yet.

Add Comment

* Required information
(never displayed)
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
Enter the third word of this sentence.
Enter answer:
Enter code:
Notify me of new comments via email.
Remember my form inputs on this computer.
I have read and understand the privacy policy. *
I have read and agree to the terms and conditions. *
Powered by Commentics