📖 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, 80
Create a variable called discount_threshold and set it to 100
Use array_filter with a closure to select prices greater than discount_threshold
Use array_map with a closure to apply a 10% discount to the filtered prices
Print 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
1
Create the prices array
Create an array called prices with these exact values: 100, 50, 200, 150, 80.
PHP
Hint
Use square brackets [] to create the array with the exact numbers inside.
2
Set the discount threshold
Create a variable called discount_threshold and set it to 100.
PHP
Hint
Just assign the number 100 to the variable $discount_threshold.
3
Filter prices above the threshold
Use array_filter with a closure to create a new array called filtered_prices that contains only prices greater than discount_threshold. Use function($price) use ($discount_threshold) as the closure.
PHP
Hint
The closure needs to access $discount_threshold using use keyword.
4
Apply discount and print results
Use array_map with a closure to create a new array called discounted_prices by applying a 10% discount to each price in filtered_prices. Then print discounted_prices using print_r.
PHP
Hint
Multiply each price by 0.9 to apply a 10% discount. Use print_r to display the array.
Practice
(1/5)
1. What is the main purpose of using a closure inside PHP array functions like array_map or array_filter?
easy
A. To define a small, unnamed function that processes array elements inline
B. To declare a global variable accessible inside the array
C. To create a new array without any function
D. To convert arrays into strings automatically
Solution
Step 1: Understand what a closure is
A closure is a small, unnamed function that can be used directly where needed.
Step 2: Role in array functions
Closures let you write the logic for processing each array element inline, making code concise and clear.
Final Answer:
To define a small, unnamed function that processes array elements inline -> Option A
Quick Check:
Closure = small inline function [OK]
Hint: Closures are small functions used directly inside array calls [OK]
Common Mistakes:
Thinking closures declare global variables
Confusing closures with array conversion functions
Assuming closures create arrays without functions
2. Which of the following is the correct syntax to use a closure with array_filter to keep only even numbers from an array $numbers?
easy
A. array_filter($numbers, function $n { return $n % 2 == 0; });
A. The closure does not return false for values <= 10, causing unexpected filtering
B. The array_filter function requires a named function, not a closure
C. The array_filter call is missing the array parameter
D. The closure syntax is invalid without a semicolon after return
Solution
Step 1: Analyze closure return behavior
The closure returns true only if value > 10, but returns nothing (null) otherwise.
Step 2: Understand array_filter behavior
array_filter expects explicit boolean returns. Returning implicit null (falsy) for values <=10 removes them but can cause unexpected filtering behavior.
Final Answer:
The closure does not return false for values <= 10, causing unexpected filtering -> Option A
Quick Check:
Closure must return true or false explicitly [OK]
Hint: Always return true or false explicitly in closures for array_filter [OK]
Common Mistakes:
Not returning false explicitly in closure
Thinking closures must be named functions
Missing array parameter in array_filter
5. Given an array $data = [1, 2, 3, 4, 5, 6], how can you use a closure with array_filter and array_map together to get an array of squares of only the odd numbers?