Extract Digit
$num % 10
Modulo gets the last digit: 86523 % 10 → 3.

This pattern builds the reverse of a number step by step, printing after each digit append. Starting from $num = 86523: 3, 32, 325, 3256, 32568. Use $num % 10 to extract digits and intdiv($num, 10) to shrink the source. This tutorial covers the loop logic, live preview, worked PHP examples, edge cases, and O(d) complexity.
$num % 10
Modulo gets the last digit: 86523 % 10 → 3.
$reverse * 10 + digit
Shift reverse left and add the digit: 0 → 3 → 32 → 325.
echo $reverse
Each iteration prints the growing reverse value.
% 10 + intdiv
Natural next step — Program 60 printed $num; this builds $reverse.
Any integer
Enter a starting number and see each progressive reverse line instantly.
Complexity
One iteration per digit — linear in the number of digits.
A progressive reverse-build pattern appends the last digit of $num to a running $reverse value and prints it each step.
In PHP: while ($num != 0), update $reverse = $reverse * 10 + ($num % 10), echo $reverse, then $num = intdiv($num, 10).
Pairing % 10 and / 10 is the core technique for reversing numbers, counting digits, and checking palindromes — the natural follow-up to Program 60.
$num % 10 extracts the last digit each step.
$reverse * 10 + digit appends to the right.
Program 60 printed $num; this prints growing $reverse.
Runs once per digit — very efficient.
In short: while $num != 0, append $num % 10 to $reverse, echo $reverse, then $num = intdiv($num, 10).
Given $num = 86523, build and echo $reverse after each digit is appended: 3, 32, 325, 3256, 32568.
$num = 86523;
$reverse = 0;
while ($num != 0) {
$reverse = $reverse * 10 + ($num % 10);
echo $reverse . PHP_EOL;
$num = intdiv($num, 10);
} | Item | Type | Description |
|---|---|---|
$num | int | Starting positive integer — loop runs while $num != 0. |
$reverse | int | Running reverse built with $reverse * 10 + ($num % 10). |
| Printed output | text | One line per iteration — growing reverse: 3, 32, 325, 3256, 32568. |
$reverse = 0
while $num != 0:
$reverse = $reverse * 10 + ($num % 10)
echo $reverse
$num = intdiv($num, 10) | Approach | Idea | Best for |
|---|---|---|
| While + modulo | $reverse * 10 + $num % 10 | Classic reverse build — Example 1 |
| fgets(STDIN) input | fgets(STDIN) for $num | User-chosen starting number |
| string accumulator | $reverse as string for large inputs | Overflow-safe variant — Example 3 |
| Program 60 contrast | Echo $num then intdiv | Removes digits instead of building reverse |
| Goal | Pattern |
|---|---|
| Init accumulator | $reverse = 0; |
| Loop condition | while ($num != 0) |
| Extract last digit | $num % 10 |
| Append to reverse | $reverse = $reverse * 10 + ($num % 10); |
| Print progressive line | echo $reverse . PHP_EOL; |
| Shrink num | $num = intdiv($num, 10); |
Three teaching angles — progressive reverse build (this program), digit removal (Program 60), and overflow-safe string $reverse.
$reverse = $reverse * 10 + $num % 10Core pattern for Examples 1 and 2.
echo $num; $num = intdiv($num, 10)Echoes shrinking $num instead of growing $reverse.
$reverse = "";Safer for very large inputs — Example 3.
build, echo, then intdivUpdate $reverse and echo before dividing $num.
Use progressive reverse-build when teaching modulo, digit extraction, and partial reverse snapshots in one while loop.
Natural next step once students can divide by 10 — now combine with % 10.
Print intermediate reverses before the classic single-line reverse program.
Pair with fgets(STDIN) — see Example 2.
Continue to Program 62 (Perfect Square Spiral) after mastering digit loops.
Console teaching pattern — builds algorithmic thinking, not screen layouts.
Key benefit: one loop that connects modulo, multiply-by-10, and progressive output.
Enter a starting number and see each progressive reverse-build line.
Three complete PHP programs — fixed $num = 86523, fgets(STDIN) input, and a string overflow-safe variant. Click View Output to reveal sample console results.
Print five progressive reverse lines from 86523: 3, 32, 325, 3256, 32568.
$num = 86523Build $reverse digit by digit and print after each append.
<?php
$num = 86523;
$reverse = 0;
while ($num != 0) {
$reverse = $reverse * 10 + ($num % 10);
echo $reverse . PHP_EOL;
$num = intdiv($num, 10);
} First iteration takes digit 3, sets $reverse to 3, then $num becomes 8652. Each step appends the next last digit until $num is 0.
Read the starting number with fgets(STDIN).
Same reverse-build loop; starting number comes from user input.
<?php
echo "Enter a number: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
echo "Invalid input." . PHP_EOL;
exit(1);
}
$num = (int) $input;
$reverse = 0;
while ($num != 0) {
$reverse = $reverse * 10 + ($num % 10);
echo $reverse . PHP_EOL;
$num = intdiv($num, 10);
} Same progressive reverse build as Example 1; only the source of $num changes.
Use a string for $reverse when inputs may be large.
Identical visual result building $reverse with string concatenation — no integer overflow.
<?php
$num = 86523;
$reverse = "";
while ($num != 0) {
$reverse .= (string) ($num % 10);
echo $reverse . PHP_EOL;
$num = intdiv($num, 10);
} String concatenation grows without integer overflow — same output for classroom-sized inputs like 86523.
$reverse = 0; and $num = 86523; before the loop.
$num % 10 reads the last digit (3, then 2, then 5, …).
$reverse = $reverse * 10 + ($num % 10); grows the reverse on the right.
echo $reverse . PHP_EOL; shows 3, 32, 325, 3256, 32568.
$num = intdiv($num, 10); removes the processed digit and moves to the next.
Exactly d lines for d digits — O(d) time, O(1) extra space.
$num = 86523Trace each loop iteration: extract digit, update reverse, print, then divide.
| Step | num | digit | reverse printed | $num after intdiv |
|---|---|---|---|---|
| 1 | 86523 | 3 | 3 | 8652 |
| 2 | 8652 | 2 | 32 | 865 |
| 3 | 865 | 5 | 325 | 86 |
| 4 | 86 | 6 | 3256 | 8 |
| 5 | 8 | 8 | 32568 | 0 (loop ends) |
The loop stops when $num becomes zero after the fifth division.
Where progressive reverse-build and digit-manipulation loops show up in PHP courses.
Same loop structure prints only the final reverse instead of each step.
Example: move echo outside the loop to print only the final reverse.
Combine % 10 and / 10 to build a reversed half for comparison.
Example: compare digits from both ends using modulo and division.
Same shrink loop counts how many digits a number has.
Example: increment a counter each iteration until num is 0.
Program 60 printed shrinking $num; this prints growing $reverse.
Example: compare both outputs side by side from the same starting value.
Linear in digit count — much faster than grid patterns for large numbers.
Example: 86523 has 5 digits → 5 loop iterations.
Pair with fgets(STDIN) and reject zero or negative input if required.
Example: use is_numeric($input) before reading input.
Pro Tip: always update and echo $reverse before dividing $num — dividing first skips the current digit.
Why progressive reverse-build belongs in beginner PHP courses.
No nested loops — natural follow-up to Program 60’s digit-removal loop.
$num % 10 and $reverse * 10 + digit are core reverse tools.
Try fgets(STDIN) input, string variant, or continue to Program 62.
Only one integer variable changes — no arrays needed.
Pro Tip: trace 86523 on paper — five lines, five digit appends, then stop.
Small habits that keep reverse-build code correct.
Always update and echo $reverse . PHP_EOL first, then $num = intdiv($num, 10).
Keep $num as int — floating-point division breaks digit removal.
while ($num != 0) stops when the last digit has been appended, printed, and divided away.
If $num starts at 0, the loop never runs — validate or show a message.
Prefer intdiv($num, 10) — bare $num /= 10 yields a float in PHP.
Pro Tip: if the first line is wrong, you divided before appending the digit to $reverse.
Mistakes that commonly break progressive reverse-build patterns.
First line shows 32 instead of 3 — you divided before appending the current digit.
→ Update $reverse and print first, then divide.
Forgetting $num = intdiv($num, 10) leaves $num unchanged forever.
→ Always update $num inside the loop body.
Using float division ($num / 10) can introduce precision issues on large values.
→ Use intdiv (not float division) for clean digit removal.
Letters or empty input leave $num uninitialized.
→ Call is_numeric($input) before casting to int.
while ($num != 0) never executes — no output at all.
→ Validate input or show a message when num is 0.
Check these inputs before calling the solution done.
Loop never runs — print nothing or show a message.
Output is one line: 8 — then num becomes 0.
Prints 120 then 12 then 1 — zero drops immediately.
Use Math.abs(num) first for clean positive output.
Call is_numeric($input) before reading $num.
Use a string $reverse if the integer may overflow — see Example 3.
Try these variations to lock in progressive reverse-build.
12345, 999, or 120$reverse once without intermediate lines$reverse — Example 3% 10 extracts the last digit; / 10 removes it — pair them in every reverse-build loop.$num != 0 for interactive programs; zero input prints nothing.$num) — this pattern prints growing $reverse.Quick Takeaway: append $num % 10 to $reverse, echo $reverse, then $num = intdiv($num, 10), repeat while $num != 0.
| Program | Time | Extra space |
|---|---|---|
| While loop (Examples 1–2) | O(d) | O(1) |
| string variant (Example 3) | O(d) | O(1) |
The progressive reverse-build pattern is a compact while-loop lesson: extract the last digit, append to $reverse, print, divide by 10, repeat until zero. Master the fixed-$num version, then try fgets(STDIN) input and the string variant in Example 3.
Practice the three examples above, then continue to Program 62 for the Perfect Square Spiral pattern.
Build then echo — loop while $num != 0 — O(d) time for d digits.
$reverse before $num = intdiv($num, 10)while ($num != 0) as the loop conditionis_numeric($input) before using fgets(STDIN) input$reverse$num inside the loopOne while loop, modulo append, O(d) time.
$reverse * 10 + digit
$num != 0
$num % 10
d lines for d digits
EdgeO(d) time
AnalysisEach step appends the last digit of $num to $reverse with $reverse = $reverse * 10 + ($num % 10), then shrinks $num with intdiv($num, 10). From 86523: 3, 32, 325, 3256, 32568 — O(d) time.
Move on to the Perfect Square Spiral pattern in the PHP number-pattern series.
12 people found this page helpful