9

this could be a very silly question but I just can't understand how PHP scope is working on this piece of code:

$leagueKey = 'NFL';
$response['response'] = array_filter($response['response'], function($tier){
    return ($tier['LeagueKey'] === $leagueKey ? true : false);
});

When I run that, I get an "Undefined variable: leagueKey" exception. On the other hand, this works perfectly well:

$response['response'] = array_filter($response['response'], function($tier){
    return ($tier['LeagueKey'] === 'NFL' ? true : false);
});

Why can't PHP see my $leagueKey variable inside the array_filter function?

Thanks!

Multitut
  • 1,937
  • 4
  • 36
  • 53
  • 2
    because `$leagueKey` is defined outside of that function. In order to use it you could use `global $leagueKey` inside your anonymous function but it's not the best of ways – ElefantPhace Sep 01 '15 at 04:50
  • Because it's out of scope, just like any function, you would have to feed an outside argument into it. `$leagueKey` is outside of the function, the string `'NFL'` is in, so it works. – Rasclatt Sep 01 '15 at 04:50
  • try this $response['response'] = array_filter($response['response'], function($tier) *use $leagueKey* { return ($tier['LeagueKey'] === $leagueKey ? true : false); }); – Ganesh Ghalame Sep 01 '15 at 04:52
  • Shouldnt be marked as duplicate.... This is a good question and different than the other one. Just a similar answer. – Andrew Nov 05 '15 at 14:07

4 Answers4

31

Your $leagueKey variable is outside the scope of the anonymous function (closure). Luckily, PHP provides a very simple way to bring variables into scope - the use keyword. Try:

$leagueKey = 'NFL';
$response['response'] = array_filter($response['response'], function($tier) use ($leagueKey) {
    return $tier['LeagueKey'] === $leagueKey;
});

This is just telling your anonymous function to "use" the $leagueKey variable from the current scope.

Edit

Exciting PHP 7.4 update - we can now use "short closures" to write functions that don't have their own scope. The example can now be written like this (in PHP 7.4):

$response['response'] = array_filter(
    $response['response'], 
    fn($tier) => $tier['LeagueKey'] === $leagueKey
);
Scopey
  • 6,120
  • 1
  • 20
  • 33
5

try this

$response['response'] = array_filter($response['response'], function($tier) use ($leagueKey) {
 return ($tier['LeagueKey'] === $leagueKey ? true : false); 
}); 
Ganesh Ghalame
  • 3,817
  • 3
  • 21
  • 27
2

This is how the scope of all the variables work. Your anonymous function doesn't know anything about variables outside of it. This holds for all kind of functions: if you need to use a variable that is outside of a function - you pass it to the function. In this case you can't pass it anything, but if you are running PHP5.3+ you can do function($tier) use ($leagueKey){ which will tell the function that it needs to use $leagueKey defined outside of it. If your php is lower than 5.3 you'll have to use workaround like this one: link

Community
  • 1
  • 1
DannyPhantom
  • 1,012
  • 9
  • 21
-4

try globals vairable:

$GLOBALS['leagueKey'] = 'NFL';
$response['response'] = array_filter($response['response'], function($tier){
    return ($tier['LeagueKey'] === $leagueKey ? true : false);
});
Ath Piseth
  • 75
  • 7
  • Using global is not a good practice check this for clarification http://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why – Ganesh Ghalame Sep 01 '15 at 05:25