PHP: Anonymous functions

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses. as example:

<?php
echo preg_replace_callback('~-([a-z])~', function ($match) {
return 
strtoupper($match[1]);
}, 
'hello-world');
// outputs helloWorld
?>

 

another one:

<?php
$greet 
= function($name)
{
printf(“Hello %s\r\n”$name);
};

$greet(‘World’);
$greet(‘PHP’);
?>

this concept is available from PHP 5.3

PHP: Anonymous functions – Manual.

Advertisements

#closures, #php