PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
14: Arrow Functions
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
usekeyword. - 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:
There are no comments for now.