Print First
echo $num
Each iteration prints the current value before shrinking it.

This pattern prints a number, then repeatedly removes the last digit with integer division by 10. Starting from 86523: 86523, 8652, 865, 86, 8. A single while loop handles it — no nested loops needed. This tutorial covers the loop logic, live preview, worked PHP examples, edge cases, and O(d) complexity.
echo $num
Each iteration prints the current value before shrinking it.
intdiv($num, 10)
Integer division drops the last digit: 86523 → 8652.
$num != 0
Loop runs once per digit until the number becomes zero.
Program 59
Natural break from nested loops — one variable, one while loop.
Any integer
Enter a starting number and see each digit-removal line instantly.
Complexity
One iteration per digit — linear in the number of digits.
A remove-last-digit pattern prints a number line by line, dropping the rightmost digit each time using intdiv($num, 10).
In PHP: while ($num != 0), echo $num, then $num = intdiv($num, 10) until the value reaches zero.
Integer division by 10 is the foundation for digit counting, reversing numbers, and palindrome checks — a natural step after grid patterns in Program 59.
No nested loops — a single while suffices.
Integer division removes the last digit each step.
Program 61 builds the reverse progressively with % 10.
Runs once per digit — very efficient.
In short: while $num != 0, echo $num, then set $num = intdiv($num, 10).
Given starting number $num = 86523, print each value as you remove the last digit until the number becomes zero.
// $num = 86523 (conceptual output)
// 86523
// 8652
// 865
// 86
// 8 | Item | Type | Description |
|---|---|---|
$num | int | Starting positive integer — must be non-zero for the loop to run. |
| Loop condition | boolean | while ($num != 0) — stops when division reaches zero. |
| Printed output | text | One line per iteration — full number, then number minus last digit, and so on. |
while $num != 0:
echo $num
$num = intdiv($num, 10) | Approach | Idea | Best for |
|---|---|---|
| While + division | intdiv($num, 10) each iteration | Classic digit-removal — Example 1 |
| fgets(STDIN) input | fgets(STDIN) for $num | User-chosen starting number |
| String substr | substr($s, 0, $len) | String practice — Example 3 |
| floor alternative | (int) floor($num / 10) | Same as intdiv for non-negative $num |
| Goal | Pattern |
|---|---|
| Set starting number | $num = 86523; |
| Loop condition | while ($num != 0) |
| Print current value | echo $num . PHP_EOL; |
| Remove last digit | $num = intdiv($num, 10); or $num = intdiv($num, 10); |
| Handle negatives | $num = abs($num); before the loop |
| Program 61 contrast | Program 61 uses % 10 to build reverse progressively |
Three ways to work with digits — this pattern uses division; Program 61 uses modulo.
$num = intdiv($num, 10)Removes last digit — used in Examples 1 and 2.
digit = num % 10Extracts last digit — used in Program 61 reverse build.
substr($s, 0, $len)Same visual output without arithmetic — Example 3.
print then divideAlways print before dividing — otherwise you skip the first value.
Reach for this pattern when teaching while loops, integer division, and digit manipulation without nested loops.
Natural break from nested loops in Program 59 — one variable, one while loop.
Simple loop condition with a clear stopping point when $num reaches zero.
Read starting number with fgets(STDIN) — see Example 2.
Compare with Program 61 (reverse build with % 10), then continue the digit series.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in while loops, integer division, and O(d) digit thinking.
Enter a starting number and see each digit-removal line in the browser.
Three complete PHP programs — fixed $num = 86523, fgets(STDIN) input, and a string-substr variant. Click View Output to reveal sample console results.
Print five lines from 86523 down to 8 using a while loop.
$num = 86523Print the current value, then divide by 10 until the number becomes zero.
<?php
$num = 86523;
while ($num != 0) {
echo $num . PHP_EOL;
$num = intdiv($num, 10);
} First iteration prints 86523, then $num becomes 8652. Each step removes one digit until $num is 0 and the loop exits.
Read the starting number with fgets(STDIN).
Same while-loop logic; 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;
while ($num != 0) {
echo $num . PHP_EOL;
$num = intdiv($num, 10);
} Same digit-removal loop as Example 1; only the source of $num changes.
Same output using substr instead of division.
Convert to string and print progressively shorter prefixes.
<?php
$num = 86523;
$s = (string) $num;
for ($len = strlen($s); $len >= 1; $len--) {
echo substr($s, 0, $len) . PHP_EOL;
} substr($s, 0, $len) prints prefixes of decreasing length — same visual result without / 10.
$num = 86523; — the value printed on the first line.
while ($num != 0) runs once per digit until division reaches zero.
echo $num . PHP_EOL outputs the current line before shrinking.
$num = intdiv($num, 10) drops the last digit: 86523 → 8652 → 865 → 86 → 8.
Exactly d lines for d digits — O(d) time, O(1) extra memory.
$num = 86523Trace each loop iteration: print, then divide by 10.
| Step | After intdiv($num, 10) | |
|---|---|---|
| 1 | 86523 | 8652 |
| 2 | 8652 | 865 |
| 3 | 865 | 86 |
| 4 | 86 | 8 |
| 5 | 8 | 0 (loop ends) |
Zero is never printed because the loop condition is checked before the next iteration.
Where integer division by 10 shows up beyond this homework pattern.
Same /10 loop counts how many digits a number has.
Example: loop until num=0 and count iterations.
Combine % 10 and / 10 — see Program 61.
Example: extract last digit with modulo, shrink with division.
Build reversed half while dividing — classic interview prep.
Example: compare original with reversed digits.
Same output with substr — see Example 3.
Example: no arithmetic, just shorter string prefixes.
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: re-prompt when user enters 0.
Pro Tip: always print before dividing — dividing first skips the original value on the first line.
Why this pattern earns a spot in beginner PHP courses.
No nested loops — easier than grid patterns in Program 59.
/ 10 and % 10 are core digit-manipulation tools.
Try string variant, negative handling, or continue to Program 61.
Only one integer variable changes — no arrays needed.
Pro Tip: trace 86523 on paper — five lines, five divisions, then stop.
Small habits that keep digit-removal code correct.
Always echo $num . 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 single digit has been printed and divided.
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 output is missing the first number, you divided before printing.
Mistakes that commonly break digit-removal patterns.
First line shows 8652 instead of 86523 — you skipped the original value.
→ Print $num first, then divide.
Forgetting $num = intdiv($num, 10) leaves $num unchanged forever.
→ Always update $num inside the loop body.
Using double can introduce precision issues on very large values.
→ Use int or long 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 abs($num) first for clean positive output.
Call is_numeric($input) before reading $num.
Use long if values exceed Integer.MAX_VALUE.
Try these variations to lock in the pattern.
12345, 999, or 120$num % 10 before dividingsubstr — Example 3/ 10 removes last digit; % 10 extracts it — pair them in Program 61.$num != 0 for interactive programs; zero input prints nothing.Quick Takeaway: echo $num, then $num = intdiv($num, 10), repeat while $num != 0.
| Program | Time | Extra space |
|---|---|---|
| While loop (Examples 1–2) | O(d) | O(1) |
| String substr (Example 3) | O(d²) | O(d) for string |
The remove-last-digit pattern is a compact while-loop lesson: print the value, 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 61 for the progressive reverse-build pattern.
Print before divide — loop while $num != 0 — O(d) time for d digits.
$num before $num = intdiv($num, 10)while ($num != 0) as the loop conditionis_numeric($input) before using fgets(STDIN) input$num inside the loopOne while loop, integer division, O(d) time.
Echo then intdiv
$num != 0
intdiv($num, 10)
Zero not printed
EdgeO(d) time
AnalysisInteger division by 10 drops the last digit each step — 86523 becomes 8652, then 865, 86, 8. One while loop, O(d) time for d digits. Prefer intdiv($num, 10) (or (int) floor($num / 10)) in PHP.
Move on to the progressive reverse-build number pattern in the PHP number-pattern series.
12 people found this page helpful