Skip to content

Exercise 10 of 10 · A complete program

Trip Summary

What you will make

A trip packing summary that reads a traveler's name from input, walks a list of item weights, flags anything over a limit, and prints a formatted total — combining everything from the earlier exercises into one program.

The one new idea: assembling a complete program from parts you already know

Real programs are rarely just one concept in isolation — they read some input, loop over data, make a decision partway through, and finish with a clean summary. This exercise is the same shape as the small utility scripts you'd actually write.

Go straight to the code ↓

Putting the pieces together

A real program is usually just the ideas you've already learned, working together in one place: a variable holding some input, a loop walking through a collection, a condition deciding something about each item, formatted text describing it, and a running total summarizing all of it by the end. Nothing here is a new idea — it's the earlier exercises, assembled.

A worked example

Here's a different small program built the same way, combining a loop, a condition, and a running total over different data:

PHP
<?php
$scores = [72, 95, 68, 88];
$passMark = 70;

$total = 0;
foreach ($scores as $score) {
    $status = "fail";
    if ($score >= $passMark) {
        $status = "pass";
    }
    echo sprintf("Score %d: %s\n", $score, $status);
    $total += $score;
}

echo sprintf("Total: %d\n", $total);

Running it prints:

Output
Score 72: pass
Score 95: pass
Score 68: fail
Score 88: pass
Total: 323

Each score is checked against $passMark inside the loop, printed with sprintf(), and added to $total on the way past. By the time the loop ends, $total holds the sum of every score, ready to print. Multiple ideas from earlier exercises, working together in one small program.

Your turn

The starter program reads a traveler's name, then walks through a packing list, flagging any item heavier than the limit as (heavy), and finishing with the trip's total weight. Right now that final total is wrong — it only reflects the last item's weight, not the sum of all of them.

This is the same accumulator pattern from an earlier exercise. Find the line inside the loop that assigns to $total, and change it so it adds the current item's weight to whatever $total already held, instead of replacing it. Then press Run.

If something goes wrong

If the total only matches the heaviest or last item in the list, the accumulator is overwriting instead of adding — the fix is the same += pattern used for any running total. If the greeting at the top prints Packing list for Explorer: even though you typed a different name into the input box, that's expected when no input reaches the program: the ?: "Explorer" fallback exists so the program always has a name to print, input or not.

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 an accumulator instead of adding to it
$total = $weight discards the running total on every pass; only += (or $total = $total + ...) actually accumulates across the whole loop.
Assuming trim(fgets(STDIN)) always returns something usable
When there's no input to read, fgets(STDIN) can come back empty, and trim() of that is just "" — the ?: "Explorer" fallback exists precisely so the program still has a sensible name to print instead of a blank one.
Losing track of which item $index is pointing at
$labels and $weights are kept in sync by using the same $index into both — $labels[$index] always names the same item that foreach just handed you as $weight, as long as $index is incremented exactly once per pass.

Want a blank editor instead? Open the PHP playground.