How does 'function' and 'use' and 'array_filter' work in PHP?

array-filter, function, php

Solution

Let's use `array_map()` to explain what's going on.

We want to duplicate the input of an array: so if the input is `aa`, the output would be `aaaa`.

So the normal way, would be to create a function and then pass it to `array_map()`:

$array = range('a', 'e');

$new_array = array_map('duplicate', $array);
print_r($new_array);

function duplicate($string){
    return $string.$string;
}

Online demo

But what if you want to use this function only once ? Since PHP 5.3, there is something called anonymous functions, we use it like the following:

$array = range('a', 'e');

$new_array = array_map(function($string){
    return $string.$string;
}, $array);
print_r($new_array);

Online demo

Now, let's say for example you want to add a standard value from another variable. That's easy with global variables. But as we know, global variables are evil and should be avoided. We may use `use()`:

$array = range('a', 'e');
$standard_value = ',';

$new_array = array_map(function($string)use($standard_value){
        // $standard_value becomes available inside the function
    return $string.$standard_value.$string;
}, $array);
print_r($new_array);

Online demo

`use()` can be become also useful if we use a reference to write to an external variable while looping:

$array = range('a', 'e');
$another_string = '';

$new_array = array_map(function($string)use(&$another_string){// note &
    $another_string .= $string.$string; // overwrite $another_string
    return $string.$string;
}, $array);
print_r($new_array);
echo PHP_EOL . $another_string;

Online demo

Problem

I am familiar with creating a PHP function placed at the top of the .php file such as: ``` function my_little_function($parm1,$parm2) { if ($parms < $parm2) { return "yes"; } else { return "no"; } } ``` Then call it like this: ``` $result = my_little_function("1","2"); echo "The answer is $result." . "\n"; ``` I have some code, I didn't write it, which uses "function" and "use" together inside of a traditional use of a function like my_little_function above. I'm puzzled by this and have some questions for you more experienced PHP developers. Here is part of the working PHP code I'm referring to: ``` $neededObject = array_filter($st_ny_trip->STOPS->STOP,function($e) use ($final_desired_dest,$connect_raw){return $e->NAME == $final_desired_dest && DateTime::createFromFormat("m/d/Y g:i:s a", $e->TIME) > $connect_raw;}); ``` `$e` is not set in any part of the function or the rest of the program, so what is using `$e`? How does it get passed a value and how is it being used? There appears to be no name for this function, so I don't know how it is being called, how is that being done? Is this creating a function, on-the-fly to be used and it gets re-generated each time this code gets called? If it's a function, why not create it outside of this function and call it? I've also not used 'use' myself yet, so that's unfamiliar to me. I looked it up on php.net and it just looks like a way to assign a value to something, but I couldn't find any practical examples to demonstrate why it's needed and when it should be used. I looked up `array_filter` and it says it's "Filters elements of an array using a callback function". I don't know what a call back function is. Is it referring to `function($e)`? Should the above line of PHP code for `$neededObject` be formatted differently so it is easier to read?

Original source