Define anonymous function in php


Introduction

Anonymous function is a function without any user defined name. Such a function is also called closure or lambda function. Sometimes, you may want a function for one time use. Closure is an anonymous function which closes over the environment in which it is defined. You need to specify use keyword in it.Most common use of anonymous function to create an inline callback function.

Syntax

$var=function ($arg1, $arg2) { return $val; };
  • There is no function name between the function keyword and the opening parenthesis.
  • There is a semicolon after the function definition because anonymous function definitions are expressions
  • Function is assigned to a variable, and called later using the variable’s name.
  • When passed to another function that can then call it later, it is known as a callback.
  • Return it from within an outer function so that it can access the outer function’s variables. This is known as a closure.

Anonymous function example

Example

 Live Demo

Output

This will produce following result. −

cube of 3 = 27

Anonymous function as callback

In following example, an anonymous function is used as argument for a built-in usort() function. The usort() function sorts a given array using a comparison function

Example

 Live Demo

 $y;
});
foreach ($arr as $x){
   echo $x . "\n";
}
?>

Output

This will produce following result. −

3
10
21
54
70

Anonymous function as closure

Closure is also an anonymous function that can access variables outside its scope with the help of use keyword

Example

 Live Demo

Output

This will produce following result. −

marks=285 percentage=95

Define anonymous function in php

Updated on 18-Sep-2020 13:35:13

  • Related Questions & Answers
  • PHP Anonymous classes
  • JavaScript closures vs. anonymous functions
  • JavaScript Encapsulation using Anonymous Functions
  • Anonymous Wrapper Functions in JavaScript
  • When to use anonymous JavaScript functions?
  • Does use of anonymous functions affect performance?
  • The Anonymous Functions in Python
  • Creating anonymous objects in PHP
  • Anonymous classes in PHP 7?
  • What are Self-Invoking Anonymous Functions in JavaScript?
  • How to pass arguments to anonymous functions in JavaScript?
  • PHP Variable functions
  • What is a typical use case for JavaScript anonymous functions?
  • PHP User-defined functions
  • What is the difference between anonymous and inline functions in JavaScript?

Summary: in this tutorial, you will learn about PHP anonymous functions and how to use them effectively.

Introduction to anonymous functions

When you define a function, you specify a name for it. Later, you can call the function by its name.

For example, to define a function that multiplies two numbers, you can do it as follows:

function multiply($x, $y) { return $x * $y; }

Code language: HTML, XML (xml)

The multiply() function accepts two arguments and returns the result. To call the multiply() function, you pass the arguments to it like this:

multiply(10, 20);

Code language: HTML, XML (xml)

In this example, the multiply() is a named function. And you can reuse it as many times as you want.

Besides named functions, PHP allows you to define anonymous functions.

An anonymous function is a function that doesn’t have a name.

The following example defines an anonymous function that multiplies two numbers:

function ($x, $y) { return $x * $y; };

Code language: HTML, XML (xml)

Since the function doesn’t have a name, you need to end it with a semicolon (;) because PHP treats it as an expression.

This anonymous function is not useful at all because you cannot use it like a named function.

To use an anonymous function, you need to assign it to a variable and call the function via the variable.

The following example assigns the anonymous function to the $multiply variable:

// ... $multiply = function ($x, $y) { return $x * $y; };

Code language: HTML, XML (xml)

And this calls the anonymous function via the $multiply variable:

echo $multiply(10, 20);

Code language: PHP (php)

When you dump the information of the $multiply variable, you’ll see that it’s actually a Clousure object:

object(Closure)#1 (1) { ["parameter"]=> array(2) { ["$x"]=>string(10) "" ["$y"]=>string(10) "" } }

Code language: PHP (php)

Note that the Closure in PHP is not the same as the closure in other programming languages such as JavaScript or Python.

Since an anonymous function is an object, you can assign it to a variable, pass it to a function, and return it from a function.

Passing an anonymous function to another function

PHP has many built-in functions that accept a callback function, for example, the array_map() function.

The array_map() function accepts a callback function and an array. It applies the callback function to each element and includes the results in a new array.

The following example shows how to double each number in an array:

function double_it($element) { return $element * 2; } $list = [10, 20, 30]; $double_list = array_map(double_it, $list); print_r($double_list);

Code language: HTML, XML (xml)

How it works.

  • First, define a named function called double_it to double a number.
  • Second, define an array of integers.
  • Third, call the array_map() function to double each element of the $list array.
  • Finally, show the result array.

Output:

Array ( [0] => 20 [1] => 40 [2] => 60 )

Code language: PHP (php)

This example works perfectly fine. However, it’s quite verbose. And the double_it function may be used once.

The following example does the same but uses an anonymous function instead:

$list = [10, 20, 30]; $results = array_map(function ($element) { return $element * 2; }, $list); print_r($results);

Code language: HTML, XML (xml)

Scope of the anonymous function

By default, an anonymous function cannot access the variables from its parent scope. For example:

$message = 'Hi'; $say = function () { echo $message; }; $say();

Code language: HTML, XML (xml)

PHP issued the following notice:

PHP Notice: Undefined variable: message in ...

Code language: plaintext (plaintext)

In this example, the anonymous function attempts to access the $message variable from its parent scope. However, it could not. Therefore, PHP issued a notice.

To use the variables from the parent scope inside an anonymous function, you place the variables in the use construct as follows:

$message = 'Hi'; $say = function () use ($message) { echo $message; }; $say();

Code language: HTML, XML (xml)

Now, it should work correctly.

Note that the $message is passed to the anonymous function by value, not by reference. If you change it inside the anonymous function, the change will not reflect outside of the function. For example:

$message = 'Hi'; $say = function () use ($message) { $message = 'Hello'; echo $message; }; $say(); echo $message;

Code language: HTML, XML (xml)

In this example, inside the anonymous function the value of the $message is 'Hello'. However, outside of the anonymous function, the value of the message remains the same as 'Hi'.

If you want to pass a variable to an anonymous function by reference, you need to use the & operator like the following example:

$message = 'Hi'; $say = function () use ($message) { $message = 'Hello'; echo $message; }; $say(); echo $message;

Code language: HTML, XML (xml)

Now, you see the 'Hello' messages twice.

Return an anonymous function from a function

The following example illustrates how to return an anonymous function from a function:

function multiplier($x) { return function ($y) use ($x) { return $x * $y; }; } $double = multiplier(2); echo $double(100); // 200 $tripple = multiplier(3); echo $tripple(100); // 300

Code language: HTML, XML (xml)

How it works.

  • First, define a function called mutiplier that returns an anonymous function.
  • Second, call the multiplier function and assign its returned value to the $double variable. Since the return value is a function, it can be invoked like a regular function ($double(2)).
  • Third, call the multiplier function and assign its returned value to the $tripple variable. This time we passed 3 instead of 2.

Summary

  • An anonymous function is a function without a name.
  • An anonymous function is a Closure object.
  • To access the variables from the parent scope inside an anonymous function, place the variables in the use construct.
  • An anonymous function can be assigned to a variable, passed to a function, or returned from a function.

Did you find this tutorial useful?

What is a anonymous function in PHP?

Anonymous function is a function without any user defined name. Such a function is also called closure or lambda function. Sometimes, you may want a function for one time use. Closure is an anonymous function which closes over the environment in which it is defined. You need to specify use keyword in it.

How do you define an anonymous function?

An anonymous function is a function that is not stored in a program file, but is associated with a variable whose data type is function_handle . Anonymous functions can accept multiple inputs and return one output. They can contain only a single executable statement.

What is anonymous function explain with example?

In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.

Where is anonymous function used?

An anonymous function is not accessible after its initial creation, it can only be accessed by a variable it is stored in as a function as a value. 3. This function is useful for all scenarios. An anonymous function can be useful for creating IIFE(Immediately Invoked Function Expression).