6

I've been learning web development using php and I'm a little bit confused about the anonymous functions. Specifically concerning the pass of parameters and how they work inside a funcion like that. For example, in the code

$array = array("really long string here, boy", "this", "middling length", "larger");
usort($array, function($a, $b) {
return strlen($a) - strlen($b);
});
print_r($array);

I don't really get how the parameters $a and $b are used. I think they're taken for comparison in order sort the array for where is defined how the function should use them and take them from?
In a code like the next one

$mult = function($x)
{
 return $x * 5;
};
echo $mult(2);

I know the parameter is passed directly to the function and used to return the result of the multiplication.
In this post the example of

$arr = range(0, 10);
$arr_even = array_filter($arr, function($val) { return $val % 2 == 0; });
$arr_square = array_map(function($val) { return $val * $val; }, $arr);

where is the variable $val taken from?

I know maybe this is not as complicated as it seems but I'm really confused about the use of the parameters on this kind of functions

Community
  • 1
  • 1
Edgar Sampere
  • 263
  • 4
  • 24
  • 1
    Generally you want to look up the docs for the outer function eg [array_map](http://php.net/manual/en/function.array-map.php) in php or [array.reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) in javascript to know what parameters get passed into the callback you're to provide. – James May 27 '16 at 16:25

2 Answers2

9
usort($array, function($a, $b) {
    return strlen($a) - strlen($b);
});

Let's take this example. When you pass a function to usort(), PHP internally calls it with 2 elements from your array to see which is bigger/smaller.

The $a and $b values come from inside the usort() function. Its code calls the provided function with 2 parameters. Your parameters don't need to be named $a and $b, they can be named whatever you like.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 2
    And if you want to call an anonymous function in an array with a specific set of parameters you can either do `$anonymous($var1, $var2)` or `$result = call_user_func($anonymous, $var1, $var2)` – h2ooooooo May 27 '16 at 16:22
  • yeah, assign it to a variable or call it directly. So basically, the two arguments are managed internally through the function, I just need to supply them and the callable will do its magic? – Edgar Sampere May 27 '16 at 16:29
  • @Kronos: The "arguments" are handled in the function, yes. Somewhere in `usort()`'s code is a line like: `$pos = $callback($arr[0], $arr[1]);`. That's where the arguments are coming from when your function is called. (Note: I just made that line up as an example, it's not *literally* in `usort()`'s code.) – gen_Eric May 27 '16 at 16:32
  • @RocketHazmat I know, dont' worry jajaja So, to manage callables within php functions, I just need to follow php's documentation, right? – Edgar Sampere May 27 '16 at 16:36
  • 1
    @Kronos: Yeah, the docs for these functions should tell you how many arguments the callable should have and what they will contain. – gen_Eric May 27 '16 at 16:37
1

Your question is not actually about anonymous functions but about passing calllables.

Let's take the first of you examples under consideration

usort($array, function($a, $b) {
return strlen($a) - strlen($b);
});

Let's refactor it a little bit by replacing anonymous function with named function.

function compareAB($a, $b) {
return strlen($a) - strlen($b);
}

usort($array, 'comapreAB');

As you see you still can ask how $a and $b are passed.

Well, the answer is very simple. usort expects you to provide callable that will take 2 arguments and it calls it internally.

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64