8

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?

2
  • 1
    php.net/manual/en/functions.anonymous.php Commented Aug 12, 2013 at 9:00
  • PHP is so awesome, it has a different argument order for array_map() and array_filter(). PHP will tell you if you're doing it wrong, but still. Weird. Commented Aug 29, 2013 at 23:07

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

Comments

4

the $e variable acts as a normal function parameter, and will thus be passed by the code calling the function, see the documentation for the value of $e when using array_filter.

The use statement imports variables from the local scope into the anonymous' function's scope.

$myvar = 'world';
$myFunc = function ($test) use ($myvar) {
    return $test . ' ' . $myvar;
};
echo $myFunc('hello'); // echoes 'hello world';

If you did not include the use ($myvar) part, then isset($myvar) would return false from inside the anonymous function, since it has a separate scope.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.