Array reduce function
📖 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, 40Create a variable called
initial and set it to 0Use
array_reduce with a callback function named sum that adds two numbersStore the result of
array_reduce in a variable called totalPrint 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