PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
In PHP, an anonymous function (also known as a closure) is a function without a name. Unlike named functions, anonymous functions can be stored in variables and passed as arguments to other functions. They are most commonly used as values for callback parameters.
Here is how you can create an anonymous function in PHP:
$greet = function($name) { echo "Hello, $name!"; }; // Call the function $greet('World'); // Outputs: Hello, World!
In this example, $greet
is a variable that holds a reference to the anonymous function. This function can then be called like any other function, using the variable name.
Closures
Closures are a special type of anonymous function. They have the ability to inherit variables from the parent scope (the function from which the closure is defined). This is done using the use
keyword.
Here's an example:
$message = "Hello, World!"; $sayHello = function() use ($message) { echo $message; }; $sayHello(); // Outputs: Hello, World!
In this example, the $sayHello
closure is able to access the $message
variable from the parent scope, even though $message
is not a parameter of the function.
Note: The use
keyword only imports the variable at the time the function is defined. If you want the closure to reflect changes to the $message
variable after the function is defined, you need to import $message
by reference, like this: use (&$message)
.
Passing Anonymous Functions as Arguments
Since anonymous functions can be stored in variables, they can also be passed as arguments to other functions. This is most commonly done with functions that accept callback parameters, like array_map
:
$numbers = [1, 2, 3, 4, 5]; $squares = array_map(function($number) { return $number * $number; }, $numbers); print_r($squares); // Outputs: Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25 )
In this example, an anonymous function is passed as the first argument to array_map
. The function is then applied to every item in the $numbers
array, and the results are stored in the $squares
array.
PHP closure function example:
$multiply = function ($x, $y) { return $x * $y; }; $result = $multiply(5, 3); echo $result; // Output: 15
PHP anonymous function use cases:
$numbers = [1, 2, 3, 4]; $squared = array_map(function ($num) { return $num * $num; }, $numbers); print_r($squared); // Output: Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 )
PHP closure function variables:
$factor = 2; $double = function ($number) use ($factor) { return $number * $factor; }; echo $double(5); // Output: 10
PHP anonymous function parameters:
$greet = function ($name) { echo "Hello, $name!"; }; $greet("John"); // Output: Hello, John!
PHP closure function return value:
$multiply = function ($x, $y) { return $x * $y; }; $result = $multiply(5, 3); echo $result; // Output: 15
PHP anonymous function vs named function:
// Anonymous function $add = function ($a, $b) { return $a + $b; }; // Named function function add($a, $b) { return $a + $b; }
PHP closure function in array_map:
array_map
to apply a function to all elements of an array.$numbers = [1, 2, 3, 4]; $squared = array_map(function ($num) { return $num * $num; }, $numbers); print_r($squared); // Output: Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 )
PHP closure function in array_filter:
array_filter
to filter elements based on a condition.$numbers = [1, 2, 3, 4]; $evenNumbers = array_filter($numbers, function ($num) { return $num % 2 == 0; }); print_r($evenNumbers); // Output: Array ( [1] => 2 [3] => 4 )