Step 1 of 5 · Reading · ~4 min
Learn
Strings
Formatting Output: printf, sprintf, Heredoc
Concatenation is fine for a sentence. It falls apart the moment you need columns that line up, a fixed number of decimal places, or a template spanning several lines. PHP borrows C's printf family for the first two, and heredoc syntax for the third.
printf writes, sprintf returns
They take identical arguments. The only difference is where the result goes.
| Sends output | Gives you a string | |
|---|---|---|
printf(...) | ✓ | no — it returns the byte count |
sprintf(...) | ✗ | ✓ |
<?php
$label = sprintf("%s is %d years old", "Alice", 30);
echo $label, "\n";
echo strlen($label), "\n";
//> Alice is 30 years old
//> 21
printf returns the number of bytes it wrote. So echo printf("hi\n"); prints hi and then 3 — a confusing double-print that catches anyone who reaches for echo out of habit. Use one or the other, never both.
The format specifiers
A specifier is %, then optional flags, then an optional width, then an optional precision, then a type letter.
| Specifier | Meaning | Result for 42 or 1.75 |
|---|---|---|
%d | integer | 42 |
%s | string | 42 |
%f | float, 6 decimals | 1.750000 |
%.2f | float, 2 decimals | 1.75 |
%5d | width 5, right aligned | 42 |
%-5d | width 5, left aligned | 42 |
%05d | width 5, zero padded | 00042 |
%x | lowercase hexadecimal | 2a |
%% | a literal percent sign | % |
Widths are what make a report readable:
<?php
printf("%-10s|%8s|%6s\n", "PARCEL", "CITY", "KG");
printf("%-10s|%8s|%6.2f\n", "P-1042", "Lisbon", 1.75);
printf("%-10s|%8s|%6.2f\n", "P-9", "Porto", 12.5);
//> PARCEL | CITY| KG
//> P-1042 | Lisbon| 1.75
//> P-9 | Porto| 12.50
printf appends nothing. Exactly like echo, the trailing "\n" is yours to put inside the format string.
Heredoc: a multi-line double-quoted string
<?php
$name = "Alice";
$msg = <<<END
Hello, $name!
Your parcel is on the way.
END;
echo $msg, "\n";
//> Hello, Alice!
//> Your parcel is on the way.
The body behaves exactly like a double-quoted string: variables interpolate, \n works, and you never have to escape a quote character. The rules for the closing marker are strict:
| Rule | Detail |
|---|---|
| Own line | The closing identifier must sit alone on its line (a ; may follow it) |
| Exact match | <<<END closes with END, nothing else, same case |
| Indentation | Allowed since PHP 7.3 — and that same indent is stripped from every body line |
| No mixing | Tabs and spaces cannot be mixed in that indentation |
The indentation rule is why the example above prints flush against the left margin even though the source is indented. This course's grader runs PHP 7.4, so indented closers are safe here; on anything older the marker has to start in column one.
Nowdoc: the single-quoted version
<?php
$raw = <<<'END'
Literal $name and \n stay exactly as typed.
END;
echo $raw, "\n";
//> Literal $name and \n stay exactly as typed.
Quote the opening identifier and nothing at all is interpolated — ideal for embedding code samples, SQL, or regular expressions.
Your exercise
Format Output reads a name and an age and prints Name: Alice, Age: 25 using printf.
The starter hands you both values. The mistake the grader catches is argument order: the format string names %s first and %d second, so passing the age before the name turns the first test's expected Name: Alice, Age: 25 into Name: 25, Age: 0 — %d applied to the word Alice prints 0, and %s happily prints a number. The second thing to know is that printf writes exactly what the format string says and nothing more, so any "\n" you want has to sit inside the quotes. (This exercise prints one line and trailing whitespace is trimmed before comparing, so it passes either way — but every multi-line exercise after this one needs its breaks.) Capital N, capital A, and a comma-and-space between the two fields.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…