<?php
// Closures are anonymous functions
// Create a math function.
function math(Closure $type, $first, $second) {
// Execute the closure with parameters
return $type($first, $second);
}
// Create an addition closure.
$addition = function ($first, $second) {
// Add the values.
return $first + $second;
};
// Create an subtraction closure.
$subtraction = function ($first, $second) {
// Subtract the values.
return $first - $second;
};
// Create a multiplication closure.
$multiplication = function ($first, $second) {
// Multiply the values.
return $first * $second;
};
$division = function ($first, $second) {
// Divide the values.
return $first / $second;
};
// Execute math function.
echo math($addition, 2, 2);
echo PHP_EOL; // New line!
echo math($subtraction, 5, 3);
echo PHP_EOL;
echo math($multiplication, 2, 4);
echo PHP_EOL;
echo math($division, 10, 3);
To embed this project on your website, copy the following code and paste it into your website's HTML: