Return to Snippet

Revision: 27412
at June 7, 2010 14:18 by jfoucher


Initial Code
<?php
 
$multiply = function($multiplier)
{
    return function($x) use ($multiplier)
    {
        return $x * $multiplier;
    };
};
 
// $mul5 now contains a function that returns a number multiplied by 5
$mult5 = $multiply(5);
 
// $mul7 contains a function that returns a number multiplied by 7
$mult7 = $multiply(7);
 
echo $mult5(5);
echo $mult7(5);
 
// Output-
// 25
// 35
 
?>

Initial URL
http://www.codediesel.com/php/anonymous-functions-in-php

Initial Description

                                

Initial Title
PHP anonymous functions and closures

Initial Tags
function

Initial Language
PHP