📖 Scenario: You are working on a simple shopping cart system. You have a list of prices for items a customer wants to buy.
🎯 Goal: You will use PHP's array_reduce function to calculate the total price of all items in the cart.
📋 What You'll Learn
Create an array called prices with the exact values: 10, 20, 30, 40
Create a variable called initial and set it to 0
Use array_reduce with a callback function named sum that adds two numbers
Store the result of array_reduce in a variable called total
Print the value of total
💡 Why This Matters
🌍 Real World
Calculating totals from lists of numbers is common in shopping carts, billing systems, and data analysis.
💼 Career
Understanding how to use array functions like <code>array_reduce</code> helps you write cleaner and more efficient PHP code in real projects.
Progress0 / 4 steps
1
Create the prices array
Create an array called prices with the exact values 10, 20, 30, and 40.
PHP
Hint
Use square brackets [] to create the array and separate values with commas.
2
Set the initial value
Create a variable called initial and set it to 0.
PHP
Hint
Just assign 0 to a variable named initial.
3
Use array_reduce to sum prices
Use array_reduce with a callback function named sum that takes two parameters and returns their sum. Store the result in a variable called total. Use $prices as the array and $initial as the initial value.
PHP
Hint
Define a function sum that adds two numbers. Then call array_reduce with $prices, the function name as a string, and $initial.
4
Print the total price
Print the value of the variable total.
PHP
Hint
Use print($total); to show the total price.
Practice
(1/5)
1. What does the PHP function array_reduce do?
easy
A. It sorts the array elements in ascending order.
B. It combines all elements of an array into a single value using a callback function.
C. It filters elements of an array based on a condition.
D. It splits an array into smaller arrays.
Solution
Step 1: Understand the purpose of array_reduce
The function takes an array and a callback to combine elements into one value.
Step 2: Compare with other array functions
Unlike sorting or filtering, array_reduce accumulates values into one result.
Final Answer:
It combines all elements of an array into a single value using a callback function. -> Option B
Quick Check:
array_reduce = combine elements [OK]
Hint: Remember: array_reduce reduces array to one value [OK]
Common Mistakes:
Confusing array_reduce with sorting functions
Thinking it filters elements instead of combining
Assuming it splits arrays into parts
2. Which of the following is the correct syntax to use array_reduce in PHP?
easy
A. array_reduce(mixed $initial = null, array $array, callable $callback)
B. array_reduce(callable $callback, array $array, mixed $initial = null)
C. array_reduce(array $array, mixed $initial = null, callable $callback)
D. array_reduce(array $array, callable $callback, mixed $initial = null)
Solution
Step 1: Recall the official function signature
The correct order is array, callback, then optional initial value.
Step 2: Check each option's parameter order
Only array_reduce(array $array, callable $callback, mixed $initial = null) matches the correct order and types.
C. Missing initial value causes $carry to be null.
D. The echo statement is missing parentheses.
Solution
Step 1: Check initial value usage
No initial value is given, so $carry starts as null.
Step 2: Understand effect on empty array
With empty array and no initial, array_reduce returns null without calling the callback.
Final Answer:
Missing initial value causes $carry to be null. -> Option C
Quick Check:
Initial value needed for empty arrays [OK]
Hint: Always provide initial value to avoid null carry [OK]
Common Mistakes:
Assuming callback parameters are reversed
Thinking array_reduce fails on empty arrays without initial
Ignoring PHP syntax for echo
5. Given an array of strings ['apple', 'banana', 'cherry'], how can you use array_reduce to create a single string with all fruits separated by commas (e.g., 'apple,banana,cherry')?
hard
A. Use array_reduce with callback concatenating carry and item with a comma, initial value as empty string.
B. Use array_reduce with callback adding carry and item as numbers, initial value 0.
C. Use array_reduce with callback returning only the last item, no initial value.
D. Use array_reduce with callback multiplying carry and item, initial value 1.
Solution
Step 1: Understand the goal
We want to join strings with commas into one string.
Step 2: Design the callback
Callback should add a comma between carry and item, starting with empty string.
Step 3: Avoid leading comma
Use a condition to avoid comma before first item or trim later.
Final Answer:
Use array_reduce with callback concatenating carry and item with a comma, initial value as empty string. -> Option A
Quick Check:
Concatenate strings with commas using array_reduce [OK]
Hint: Concatenate strings with commas, start with empty string [OK]
Common Mistakes:
Adding strings as numbers causing errors
Not providing initial value causing null carry
Using multiplication on strings
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.