$pagetitle="PHP functional programming."; $pageid=99; $sitelink="http://www.learn-coding.today/php_closures.php"; $page="vb_keeper2.php"; include('includes/headerzero.php'); ?>
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 function($f){ //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); OUTPUT: 35 //Call $C passing getF() function as a parameter echo $C(getF(95)); OUTPUT: 35
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.
$F=90; // Create a Closure function convert($f) { return function() use ($f) { echo (round(($f-32)*(5/9),0)); }; } $converter = convert($F); $converter(); OUTPUT: 32
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); // OUTPUT: 35
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); OUTPUT: 30
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); OUTPUT: 42
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); OUTPUT: 54
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.
Our dog needs urgent surgery, and the cost is overwhelming.
Any help, big or small, would mean the world to us. Thank you for supporting Oscar on his journey to recovery!
Oscar Story.
Oscar wasn’t just any puppy—he was a gift from a mother who trusted us with her smallest one.
For five years, my wife worked at the Indian Medical Center in Arizona, deep in Navajo Nation. Near her clinic, she often saw a homeless dog wandering the area. Over time, she began feeding her, and the dog grew fond of her. Then, one day, that same dog brought her newborn puppies to my wife—as if proudly showing them off.
Among them was the smallest, most delicate pup. My wife couldn’t resist. She brought him home, and we named him Oscar.
Oscar thrived in the house provided by the medical center, enjoying the big backyard where he lived. I built him a sturdy wooden doghouse, and we often took him on walks along the Window Rock Trail. He became our adventure companion, making the vast desert feel like home.
After my wife’s contract ended, we moved back to Florida, bringing Oscar with us. He adjusted to his new surroundings, but he never lost his adventurous spirit.
Now, Oscar faces a tough challenge—he needs urgent surgery, and the cost is overwhelming. We want to give him the best care possible, just as he’s given us years of joy and loyalty.
Any help, big or small, would mean the world to us. Thank you for supporting Oscar on his journey to recovery!