3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Example 1 -- Unpacking the array to do work on it array_map(function ($x) { $first_name = $x[0]; $last_name = $x[1]; // Do important work here with first_name and last_name... // But $first_name and $last_name are scoped to this function // See the second example for how we'd inject an anonymous function to operate with $first_name and $last_name }, [ ['John', 'Galt'], ['Howard', 'Roark'], ]); // Example 2 -- Separation of input setup and behavior $behavior = function ($first_name, $last_name) { var_dump('Doing hard work!', $first_name, $last_name); }; array_map(function ($x) use ($behavior) { $first_name = $x[0]; $last_name = $x[1]; // $behavior is invoked using our unpacked arguments $behavior($first_name, $last_name); }, [ ['John', 'Galt'], ['Howard', 'Roark'], ]);

preferences:
31 ms | 405 KiB | 5 Q