📖 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, 6
Create a variable called total and set it to 0
Use 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 time
Inside the loop, add $quantities[$i] to $total
Print 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
1
Create the quantities array
Create an array called quantities with these exact values: 3, 5, 2, 8, 6.
PHP
Hint
Use square brackets [] to create an array in PHP.
2
Create the total variable
Create a variable called total and set it to 0.
PHP
Hint
Use $total = 0; to create and set the variable.
3
Add a for loop to sum quantities
Use a for loop with variable i starting at 0, running while i is less than count($quantities), and incrementing i by 1 each time. Inside the loop, add $quantities[$i] to $total.
PHP
Hint
Use count($quantities) to get the number of items in the array.
4
Print the total
Print the value of $total using echo.
PHP
Hint
Use echo $total; to display the total sum.
Practice
(1/5)
1. What is the role of the condition part in a PHP for loop?
easy
A. It decides if the loop should continue running or stop.
B. It sets the starting value of the loop variable.
C. It changes the loop variable after each iteration.
D. It runs only after the loop finishes.
Solution
Step 1: Understand the for loop parts
A PHP for loop has three parts: initialization, condition, and increment.
Step 2: Identify the condition's role
The condition is checked before each loop run to decide if the loop continues or stops.
Final Answer:
It decides if the loop should continue running or stop. -> Option A
Quick Check:
Condition controls loop continuation = It decides if the loop should continue running or stop. [OK]
Hint: Condition controls loop stop or continue [OK]
Common Mistakes:
Confusing initialization with condition
Thinking increment runs before loop starts
Believing condition runs after loop ends
2. Which of the following is the correct syntax for a PHP for loop that counts from 0 to 4?
easy
A. for $i = 0; $i <= 4; $i++ {}
B. for ($i = 0; $i < 4; $i--) {}
C. for ($i = 0; $i <= 4; ++i) {}
D. for ($i = 0; $i <= 4; $i++) {}
Solution
Step 1: Check for loop syntax rules
PHP for loops require parentheses around initialization, condition, and increment, separated by semicolons.
Step 2: Validate each option
for ($i = 0; $i <= 4; $i++) {} uses correct syntax with $i starting at 0, condition $i <= 4, and increment $i++. Others have syntax errors or wrong increments.
Final Answer:
for ($i = 0; $i <= 4; $i++) {} -> Option D
Quick Check:
Correct for loop syntax = for ($i = 0; $i <= 4; $i++) {} [OK]
Hint: Use parentheses and semicolons correctly in for loops [OK]