Closures in array functions
📖 Scenario: You have a list of product prices in an online store. You want to apply a discount to some products based on their price.
🎯 Goal: Use a closure (anonymous function) with PHP's array_filter and array_map functions to select products above a certain price and apply a discount.
📋 What You'll Learn
Create an array called
prices with the exact values: 100, 50, 200, 150, 80Create a variable called
discount_threshold and set it to 100Use
array_filter with a closure to select prices greater than discount_thresholdUse
array_map with a closure to apply a 10% discount to the filtered pricesPrint the resulting discounted prices array
💡 Why This Matters
🌍 Real World
Online stores often need to apply discounts to products based on price or other conditions. Using closures with array functions helps write clean and reusable code.
💼 Career
Understanding closures and array functions is important for PHP developers working on e-commerce sites, data processing, and backend logic.
Progress0 / 4 steps