IMPRESS dotted_line CONTACT dotted_line search dotted_line Kontakt dotted_line sitemap dotted_line print
134 PHP-Resource users online

Switch to another languags Deutsch aktuelle Sprache Englisch

PHP Manual

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.

Example #1 Anonymous function example

echo preg_replace_callback('~-([a-z])~', function ($match) {
// outputs helloWorld

Closures can also be used as the values of variables; PHP automatically converts such expressions into instances of the Closure internal class. Assigning a closure to a variable uses the same syntax as any other assignment, including the trailing semicolon:

Example #2 Anonymous function variable assignment example

= function($name)
printf("Hello %s\r\n"$name);


Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header. Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared (not necessarily the function it was called from). See the following example:

Example #3 Closures and scoping

// A basic shopping cart which contains a list of added products
// and the quantity of each product. Includes a method which
// calculates the total price of the items in the cart using a
// closure as a callback.
class Cart
PRICE_MILK    3.00;
PRICE_EGGS    6.95;

$products = array();
    public function 
$this->products[$product] = $quantity;
    public function 
        return isset(
$this->products[$product]) ? $this->products[$product] :
    public function 
$total 0.00;
$callback =
            function (
$quantity$product) use ($tax, &$total)
$pricePerItem constant(__CLASS__ "::PRICE_" .
$total += ($pricePerItem $quantity) * ($tax 1.0);

$my_cart = new Cart;

// Add some items to the cart

// Print the total with a 5% sales tax.
print $my_cart->getTotal(0.05) . "\n";
// The result is 54.29

Anonymous functions are implemented using the Closure class.


Version Description
5.4.0 $this can be used in anonymous functions.
5.3.0 Anonymous functions become available.


Note: It is possible to use func_num_args(), func_get_arg(), and func_get_args() from within a closure.

Comments to the PHP manual
Write new comment


New Tutorial entries

Migration einer PHP 5 App auf PHP 7

Dieses PHP 7 Tutorial zeigt dir, wie du dein PHP5 Script auf PHP7 umstellst.

Berni | Category: PHP
PHP 7 Virtual Machine

Dieser Artikel zielt darauf ab, einen Überblick über die Zend Virtual Machine, wie es in PHP 7 gefunden wird.

Berni | Category: PHP
plotting masters - a professional guide - Teil II

Grafische Interpolation und Bestapproximation von numerischen Wertepaaren: Wir wollen Punkte auf einer Zeichenebene über verschiedene Verfahren miteinander verbinden.

EVAMasters | Category: PHP