php - Is there any difference between passing variables into function and import them by USE? -


take @ code:

$str = implode(', ', array_map(function($var1) use ($var2) { /* code */ }, $arr)); 

as see i've used use keyword import var2 in function. ok fine. can without use either. this:

$str = implode(', ', array_map(function($var1. $var2) { /* code */ }, $arr)); 

so when should use use ? has specific advantage?

the reason use use set variable value @ time of function definition rather when function called. example,

$var1 = 1; $var2 = 2; $fun1 = function($var1) use ($var2) { return $var1 + $var2; }; $fun2 = function($var1, $var2) { return $var1 + $var2; };  echo $fun1(3); // prints 5 echo $fun2(3,4); // prints 7