Exercise 7 of 10 · Strings and formatting
Score Report
What you will make
A neatly formatted line of text with a player's ID, name and score lined up using sprintf(), instead of stitched together with dots.
The one new idea: sprintf() builds a formatted string from a template
Real output usually needs to look tidy — lined up, rounded, padded. sprintf() (and its cousin printf()) build formatted text in one step instead of many small concatenations, and they show up everywhere from receipts to log files.
Go straight to the code ↓Building formatted text with sprintf()
sprintf() builds a string from a template plus a list of values, and hands the finished string back to you (unlike printf(), which prints it directly). The template contains placeholders called format specifiers — %s for a string, %d for an integer — that get replaced, in order, by the values passed after the template.
A specifier like %03d adds two extra instructions to %d: pad the result with 0, out to a minimum total width of 3 characters. sprintf("%03d", 7) becomes "007"; sprintf("%03d", 87) becomes "087", because 87 is already two digits and only needs one more zero to reach that width. Without the padding, a mix of one-, two-, and three-digit numbers would print at different widths and never line up.
A worked example
Here's a different formatted line, using a different specifier, so you can see the mechanics without today's answer:
<?php
$item = "eggs";
$qty = 6;
echo sprintf("%-10s qty: %02d\n", $item, $qty);Running it prints:
eggs qty: 06%-10s pads "eggs" out to 10 characters, left-aligned because of the -, and %02d pads 6 to at least 2 digits with a leading zero. Each specifier in the template controls its own value, independently of the others.
Your turn
The starter program is supposed to print a player's ID padded to exactly 3 digits — like 007 — followed by their name and score. Right now the ID prints with no padding at all.
Look at the width number inside the ID's format specifier. It currently pads to a minimum of one character, which does nothing for a number that's already one digit long. Change it to the width the comment above the code asks for, then press Run.
If something goes wrong
If the ID still isn't padded, double-check the number sitting between the 0 and the d in the specifier — that number sets the total width of the result, not the number of zeros to add on top. If the score or name print incorrectly, make sure %d lines up with the numeric value passed to sprintf() and %s lines up with the text value, in the same order they appear after the template.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
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
- Getting the padding width wrong in a %d specifier
- The number right after the 0 in %0Nd sets the minimum total width, not the number of zeros to add — %03d means 'at least 3 characters total', so sprintf("%03d", 7) gives "007" and sprintf("%03d", 87) gives "087".
- Using %s for a number that needs formatting
- %s prints a value as plain text with no padding, rounding or zero-filling applied — it happens to work for a plain integer, but it can't add the leading zeros a %0Nd specifier can.
- Forgetting sprintf() returns a string instead of printing it
- sprintf() builds and hands back the formatted string; unlike printf(), it doesn't print anything by itself, so the result still needs to go through echo or be used some other way.
Want a blank editor instead? Open the PHP playground.