For loop execution model
📖 Scenario: You are helping a small shop owner count how many items they have in stock. They have a list of item quantities and want to add them up using a for loop.
🎯 Goal: Build a PHP program that uses a for loop to add up all the quantities in an array and print the total.
📋 What You'll Learn
Create an array called
quantities with the exact values: 3, 5, 2, 8, 6Create a variable called
total and set it to 0Use a
for loop with a variable i starting at 0, running while i is less than the length of quantities, and incrementing i by 1 each timeInside the loop, add
$quantities[$i] to $totalPrint the value of
$total💡 Why This Matters
🌍 Real World
Counting items in stock is a common task in shops and warehouses to keep track of inventory.
💼 Career
Understanding loops and arrays is essential for programming tasks like data processing, reporting, and automation.
Progress0 / 4 steps