Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

I was working on a small product catalog tool the other day, and I found myself writing a lot of repetitive code to filter prices. I wanted to take an array of products and keep only the ones that fell under a specific budget set by the user. Since I already knew how to use array_filter, I started with a standard anonymous function.

The boilerplate burden

Here is what my first attempt looked like. It works, but look at that use keyword. It always felt like a bit of a speed bump to me:

$maxBudget = 50;
$products = [
    ['name' => 'USB Cable', 'price' => 12],
    ['name' => 'Mechanical Keyboard', 'price' => 85],
    ['name' => 'Gaming Mouse', 'price' => 45],
    ['name' => '4K Monitor', 'price' => 300],
];

$affordableProducts = array_filter($products, function($product) use ($maxBudget) {
    return $product['price'] <= $maxBudget;
});

If you've written a few closures in PHP, you know the drill: if the function needs a variable from the outside scope, you have to explicitly "import" it using use. It's fine for one variable, but when you're importing four or five, the function signature becomes an eyesore. I started wondering if there was a way to just... let the function see the variable.

The 'fn' shortcut

This is where arrow functions come in. I decided to rewrite that filter using the fn keyword. I stripped out the use statement and the curly braces, and replaced the return with a fat arrow (=>).

$maxBudget = 50;
$products = [
    ['name' => 'USB Cable', 'price' => 12],
    ['name' => 'Mechanical Keyboard', 'price' => 85],
    ['name' => 'Gaming Mouse', 'price' => 45],
    ['name' => '4K Monitor', 'price' => 300],
];

// Much cleaner, right?
$affordableProducts = array_filter($products, fn($product) => $product['price'] <= $maxBudget);

I noticed two things immediately. First, the $maxBudget variable is captured automatically. I didn't have to tell PHP to "use" it; the arrow function just reached out and grabbed it from the parent scope. Second, the return is implicit. I didn't have to write return because the expression to the right of the arrow is automatically returned.

Where the magic (and the limits) are

Now, as a software engineer, I'm always suspicious of "magic" scope capturing. I wanted to see if I could modify that $maxBudget variable from inside the arrow function. I tried to increment it inside the filter to see if it would affect the original variable:

$maxBudget = 50;
$test = array_filter($products, fn($p) => $maxBudget++); 
// Wait, this doesn't work the way I thought.

I quickly realized that arrow functions capture variables by value. Even if I change the variable inside the function, the original $maxBudget outside stays exactly as it was. If you need to modify a variable in the parent scope, you're stuck with the traditional function() use (&$var) syntax using a reference.

I also tried to add a var_dump inside the arrow function to debug the prices, but PHP threw a syntax error. That's the trade-off: arrow functions are limited to a single expression. You can't have a block of code with multiple lines, if/else statements (unless they are ternary), or loops. They are designed for short, one-liner transformations.

  • Use them when you have a simple calculation or a boolean check.
  • Use them to avoid the clunkiness of the use keyword.
  • Stick to traditional anonymous functions if you need multi-line logic or need to modify variables by reference.



📋 Practical Task

Low-Stock Inventory Alert Filter

You have an array of warehouse items, each with a name and a current quantity. You have a variable called $threshold that defines what "low stock" means.

Your task is to use array_filter and an arrow function to create a new array containing only the items whose quantity is strictly less than the $threshold.

$threshold = 10;
$inventory = [
    ['item' => 'Laptop Sleeves', 'qty' => 25],
    ['item' => 'HDMI Cables', 'qty' => 8],
    ['item' => 'Webcams', 'qty' => 12],
    ['item' => 'USB-C Hubs', 'qty' => 3],
    ['item' => 'Mousepads', 'qty' => 15],
];

// Write your arrow function filter here:

Rating
0 0

There are no comments for now.

to be the first to leave a comment.