Anonymous functions, also called closures, allow you to temporarily create a function without a specified name. The value most commonly used as a callback function argument.
Closures can inherit variables from the parent scope. Any such variables should be passed in using the use language construct.
Used in built-in functions and custom functions respectively, sample code:
//递归为数组的每个元素应用回调函数 $arr = [ 'name' => ' joker', 'content' => [ 'date' => ' 2018-03-07 11:11:11', 'doing' => ' 测试匿名函数 ' ]];function array_map_recursive(callable $func, array $array) { return filter_var($array, FILTER_CALLBACK, ['options' => $func]);}//调用示例$prefix = '前缀:';$arr1 = array_map_recursive(function($v) use($prefix) { return $prefix.trim($v);}, $arr);$arr2 = array_map(function($v) { return is_string($v)?trim($v):$v;}, $arr);
Related recommendations:
A summary of several PHP anonymous function usage examples
PHP anonymous functions and closures
Usage of php anonymous functions
The above is the detailed content of Detailed explanation of PHP anonymous functions and anonymous classes. For more information, please follow other related articles on the PHP Chinese website!