Skip to content

Exercise 9 of 10 · Putting it together

Total Weight

What you will make

The combined weight of every item in a packing list, added up one item at a time inside a loop.

The one new idea: a variable before a loop can carry a running total

Summing, counting and averaging are some of the most common things a loop does. The pattern here — a variable started before the loop, updated on every pass — is the same shape used for totals, counts and averages throughout real programs.

Go straight to the code ↓

Accumulating a running total

A running total is a value that starts before a loop begins and gets updated on every pass, ending up with the combined result once the loop finishes. The pattern has two parts: create the variable once, outside and before the loop, starting at 0; then, inside the loop, add to it rather than replace it — $total = $total + $value;, or the shorter $total += $value;.

Miss either half and the pattern breaks. Create the variable inside the loop and it resets on every pass, back to its starting value. Replace it instead of adding to it, and only the very last value ever survives to the end.

A worked example

Here's a different total, using different numbers, so you can trace the pattern fresh:

PHP
<?php
$prices = [3, 7, 2];

$total = 0;
foreach ($prices as $price) {
    $total += $price;
}

echo "Total: $total\n";

Running it prints:

Output
Total: 12

$total starts at 0. Each pass through the loop adds the current $price to whatever $total already held — 0 + 3 = 3, then 3 + 7 = 10, then 10 + 2 = 12 — so by the time the loop ends, $total holds the sum of all three prices.

Your turn

The starter program is supposed to add up the weight of every item in a packing list. Right now, the total printed at the end is far too small — it only matches the last item's weight.

Look at what the loop does to $total on each pass. Change it so $total gets the current weight added to whatever it already held, instead of being replaced by it, then press Run.

If something goes wrong

If the total only matches the last item in the array, the loop is overwriting $total instead of adding to it — look for a line shaped like $total = $weight; and change it to add instead. If the total is 0 no matter what, check that $total = 0; is written before the loop starts, not repeated somewhere inside it, where it would reset the total on every single pass.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Overwriting the accumulator instead of adding to it
$total = $weight throws away the running total on every pass and replaces it with just the current value — by the time the loop ends, only the very last item's weight survives.
Creating the total variable inside the loop
$total = 0; written inside the loop body resets it back to 0 on every single pass, which has the same effect as never accumulating anything at all. It has to be created once, before the loop starts.
Forgetting to print the variable after the loop finishes
The addition itself doesn't display anything — echo has to run after the loop ends, using the same $total variable the loop was updating.

Want a blank editor instead? Open the PHP playground.