Lamda Functions

Lambda functions or “anonymous functions,” as they are often referred to, are simply throwaway functions that can be defined at any time and are typically bound to a variable. The functions themselves only exist in the scope of the variable of which they are defined, so when that variable goes out of scope, so does the function.

Example:

$double = function($num) { return $num * 2; }

Lambda functions are useful for a number of instances, most notably for many PHP functions that accept a callback function. One such function is array_map(), which allows us to walk through an array and apply a callback function to each element of the array.

Comments are closed.