<?php
// 使用匿名函数语法
$double1 = function ($a) {
return $a * 2;
};
// 使用一级可调用语法
function double_function($a) {
return $a * 2;
}
$double2 = double_function(...);
// 使用箭头函数语法
$double3 = fn($a) => $a * 2;
// 使用 Closure::fromCallable
$double4 = Closure::fromCallable('double_function');
// 此处使用 closure 作为回调,将范围内每个元素的值翻倍。
$new_numbers = array_map($double1, range(1, 5));
print implode(' ', $new_numbers) . PHP_EOL;
$new_numbers = array_map($double2, range(1, 5));
print implode(' ', $new_numbers) . PHP_EOL;
$new_numbers = array_map($double3, range(1, 5));
print implode(' ', $new_numbers) . PHP_EOL;
$new_numbers = array_map($double4, range(1, 5));
print implode(' ', $new_numbers);
?>
2 4 6 8 10
2 4 6 8 10
2 4 6 8 10
2 4 6 8 10