📖 Scenario: You are working on a small PHP script that processes a list of product prices. You want to apply a discount to each price and then display the discounted prices.
🎯 Goal: Build a PHP script that uses arrow functions (short closures) to apply a discount to each product price and then prints the discounted prices.
📋 What You'll Learn
Create an array called prices with the exact values: 100, 200, 300
Create a variable called discount and set it to 0.1 (which means 10%)
Use an arrow function inside array_map to calculate discounted prices
Print the resulting discounted prices array
💡 Why This Matters
🌍 Real World
Applying discounts or taxes to product prices is common in online stores and billing systems.
💼 Career
Understanding arrow functions helps write concise and modern PHP code, which is valuable for web developers.
Progress0 / 4 steps
1
Create the prices array
Create an array called prices with the exact values 100, 200, and 300.
PHP
Hint
Use square brackets [] to create an array in PHP.
2
Set the discount rate
Create a variable called discount and set it to 0.1 to represent a 10% discount.
PHP
Hint
Use the = sign to assign the value 0.1 to the variable discount.
3
Calculate discounted prices using an arrow function
Use array_map with an arrow function to create a new array called discountedPrices. The arrow function should take each price and return the price after subtracting the discount (price minus price times discount).
PHP
Hint
Use fn($price) => $price - $price * $discount as the arrow function inside array_map.
4
Print the discounted prices
Print the discountedPrices array using print_r to display the discounted prices.
PHP
Hint
Use print_r($discountedPrices); to print the array in a readable format.
Practice
(1/5)
1. What is a key feature of PHP arrow functions (short closures)?
easy
A. They automatically return the value of the expression without needing the return keyword.
B. They require the use of the 'function' keyword every time.
C. They cannot access variables from the parent scope.
D. They must always have multiple statements inside the function body.
Solution
Step 1: Understand arrow function syntax
Arrow functions in PHP use the 'fn' keyword and automatically return the expression value without needing 'return'.
Step 2: Compare features
Unlike regular closures, arrow functions can access variables from the parent scope without 'use'.
Final Answer:
They automatically return the value of the expression without needing the return keyword. -> Option A
Quick Check:
Arrow functions auto-return expression [OK]
Hint: Arrow functions auto-return expressions without 'return' keyword [OK]
Confusing arrow functions with regular anonymous functions
2. Which of the following is the correct syntax for a PHP arrow function that doubles a number?
easy
A. $double = function($x) { $x * 2; };
B. $double = fn($x) => $x * 2;
C. $double = fn $x => return $x * 2;
D. $double = fn($x) { return $x * 2; };
Solution
Step 1: Identify correct arrow function syntax
Arrow functions use 'fn' keyword, parentheses for parameters, and '=>' followed by expression without braces.
Step 2: Check each option
$double = fn($x) => $x * 2; matches correct syntax: fn($x) => $x * 2;. The other options use regular function syntax, braces incorrectly, or lack parentheses and misuse 'return'.
Final Answer:
$double = fn($x) => $x * 2; -> Option B
Quick Check:
Arrow function syntax = fn(params) => expression [OK]
Hint: Arrow functions use 'fn(params) => expression;' syntax [OK]
Common Mistakes:
Using braces {} with arrow functions
Adding 'return' keyword inside arrow functions
Omitting parentheses around parameters
3. What will be the output of the following PHP code?