During PHP development, array_filter is a commonly used array processing function, which allows developers to filter elements in an array through callback functions. The basic syntax of this function is simple, but there is a detail that is easily overlooked: when referencing external variables in an anonymous function, the variable must be explicitly passed in using the use keyword . Otherwise, the code may not work as expected, and may even lead to logical errors. This article will analyze this problem in depth and illustrate with examples why use must be used.
The function of array_filter is to use the callback function to filter the values in the array, and only the elements that the callback function returns true . The basic usage is as follows:
$input = [1, 2, 3, 4, 5];
$output = array_filter($input, function($value) {
return $value > 3;
});
// Output: [4, 5]
In the above code, array_filter filters out values greater than 3 through anonymous functions.
Let’s look at a more complex scenario. We need to filter the values in the array based on a dynamic condition. This condition is determined by the external variable $threshold :
$threshold = 3;
$input = [1, 2, 3, 4, 5];
$output = array_filter($input, function($value) {
return $value > $threshold; // ? An error or a problem will occur here
});
When running this code, PHP will prompt Undefined variable: threshold . This is because the anonymous function scope does not directly access variables whose external scope .
To solve the above problem, PHP provides the use keyword, which allows you to introduce variables from external scopes into the local scope of anonymous functions:
$threshold = 3;
$input = [1, 2, 3, 4, 5];
$output = array_filter($input, function($value) use ($threshold) {
return $value > $threshold;
});
// Output: [4, 5]
Through use ($threshold) , the anonymous function can correctly access the $threshold variable.
When you use use to introduce external variables in anonymous functions, PHP is actually passing these variables into the function as a value . That is to say, if you modify $threshold in an anonymous function, it will not affect the original variable.
Let’s take a look at an example:
$threshold = 3;
$filter = function($value) use ($threshold) {
$threshold = 10;
return $value > $threshold;
};
echo $threshold; // 仍然Output 3
If you want to modify the value of an external variable in an anonymous function, you need to pass it using a reference:
$threshold = 3;
$filter = function($value) use (&$threshold) {
$threshold = 10;
return true;
};
$filter(5);
echo $threshold; // Output 10
Imagine you are working on a list of articles and you want to filter out articles for a specific category, and the category ID is present in an external variable. If you forget to use use , you may get an empty array:
$categoryId = 2;
$articles = [
['id' => 1, 'category_id' => 1],
['id' => 2, 'category_id' => 2],
['id' => 3, 'category_id' => 3],
];
$filtered = array_filter($articles, function($article) {
return $article['category_id'] == $categoryId;
});
print_r($filtered); // ? mistake,$categoryId Undefined
After using use instead:
$filtered = array_filter($articles, function($article) use ($categoryId) {
return $article['category_id'] == $categoryId;
});
// ? Correct filtering,The result is ID for 2 Articles
When using array_filter and other higher-order functions (such as array_map , array_reduce , etc.), if you need to use external variables in the callback function, be sure to use the use keyword to explicitly pass these variables. Otherwise, you will not be able to access these variables, which may cause code errors or logical exceptions.
This is an important feature of closure scope in PHP, understanding it allows you to write more robust and clearer code.
I hope this article can help you avoid this small trap in daily development. If you encounter similar problems when using closures, you might as well check whether the use is missing!