PHP Number Pattern (Remove Last Digit)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A remove-last-digit pattern prints a number, then drops its rightmost digit each line — 86523 → 8652 → 865 → 86 → 8 — until nothing remains.

Remember
Rule: while num != 0
        print num
        num = intdiv(num, 10)

86523
8652
865
86
8     ← start = 86523

Unlike nested-loop triangles (Programs 1–59), this uses one while loop and integer division. Program 61 builds a number by reversing digits progressively.

How to Solve It

Print the current value, then set $num = intdiv($num, 10). Repeat while $num != 0. Prefer intdiv over float division so results stay exact integers.

MethodIdeaBest for
Fixed numberwhile + intdiv($num, 10)Labs and demos
CLI inputSame loop; read $num at runtimeInteractive practice
String prefixessubstr($s, 0, $len) shrinking lengthAvoiding division

Pseudocode

Pseudocode
num = starting value
while num != 0:
    print num and newline
    num = integer divide num by 10

Cheat sheet

GoalPattern
Set start$num = 86523;
Loopwhile ($num != 0) { ... }
Print lineecho $num . PHP_EOL;
Drop last digit$num = intdiv($num, 10);
String alternativeecho substr($s, 0, $len) . PHP_EOL;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $numStays on the same lineRare here — usually you want a full line
echo $num . PHP_EOLPrints value and ends the lineEach shortened number (this pattern)

Same idea as C# WriteLine: one number per line. Nested-row patterns use echo for digits and echo PHP_EOL after the row.

Live Preview

Change the starting number and each digit-removal line updates instantly.

Positive whole numbers up to 9 digits. Tap a chip or type a value — the preview redraws as you go.

Live result start = 86523 · lines = 5
86523
8652
865
86
8

Worked Walkthrough

Trace $num = 86523 — each step prints, then divides by 10.

IterationPrintAfter intdiv(..., 10)
1865238652
28652865
386586
4868
580 → loop ends

0 is never printed — the while condition fails before that iteration.

PHP Programs

Three complete programs: fixed $num = 86523, CLI input, and a substr variant. Use View Output for sample results.

Example 1 — Fixed $num = 86523

Print the current value, then divide by 10 until the number becomes zero.

PHP
<?php
$num = 86523;

while ($num != 0) {
    echo $num . PHP_EOL;
    $num = intdiv($num, 10);
}
?>

How It Works

1. Print first. Each iteration outputs the current $num before shrinking it.

2. Drop a digit. intdiv($num, 10) removes the last digit (discarding the remainder).

3. Stop at zero. When division yields 0, while ($num != 0) ends — 0 is not printed.

Example 2 — User Input

Read the starting number from the CLI. Prefer is_numeric (shown below).

PHP
<?php
echo "Enter a number: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input == 0) {
    echo "Enter a non-zero whole number." . PHP_EOL;
    exit(1);
}
$num = abs((int) $input);

while ($num != 0) {
    echo $num . PHP_EOL;
    $num = intdiv($num, 10);
}
?>

How It Works

1. Validate input. is_numeric rejects letters; reject zero so the loop would otherwise print nothing.

2. Use abs. abs((int) $input) keeps lines positive if the user types a negative value.

3. Same while loop. Print and intdiv match Example 1 — only the source of $num changes.

Example 3 — String Substr

Same output using string prefixes instead of division.

PHP
<?php
$num = 86523;
$s = (string) $num;

for ($len = strlen($s); $len >= 1; $len--) {
    echo substr($s, 0, $len) . PHP_EOL;
}
?>

How It Works

1. Convert once. Store the digits as a string so length is known up front.

2. Shorten prefixes. substr($s, 0, $len) keeps the leftmost $len characters.

3. Same look. Visual result matches Examples 1–2 without intdiv.

Edge Cases & Pitfalls

Check these before calling the solution done.

float /

Using $num / 10 without intdiv

In PHP, / can yield floats. Prefer intdiv($num, 10) (or (int) ($num / 10)) for clean integers.

divide first

Dividing before printing

That skips the full starting number. Print first, then intdiv.

num = 0

Starting at zero

The loop never runs — no output. Validate and ask for a non-zero value.

trailing 0

Numbers ending in zero

120 → 12 → 1. Trailing zeros disappear in one step like any last digit.

negatives

Negative input

Without abs, lines may show a minus. Example 2 takes the absolute value first.

single digit

One-digit start

Output is a single line (e.g. 7), then division yields 0 and the loop stops.

Time and Space Complexity

ProgramTimeExtra space
while + intdiv (Examples 1–2)O(d)O(1)
String substr (Example 3)O(d²) worst case for substr copiesO(d) for the string

For d digits, the while-loop version runs exactly d iterations — linear in the digit count, not in the numeric magnitude alone.

Key Takeaways

  • Rule: print, then intdiv($num, 10), until $num is 0.
  • Prefer intdiv: keeps each step an exact integer without float leftovers.
  • echo + PHP_EOL: one call per line prints the value and advances.
  • Next step: Program 61 builds a number by reversing digits step by step.

One line: print the number, divide by 10, repeat until nothing is left.

Frequently Asked Questions

Integer division discards the remainder. So intdiv(86523, 10) becomes 8652, intdiv(8652, 10) becomes 865, and so on.
The loop prints $num then divides. When $num becomes 0, while ($num != 0) is false — 0 is never printed.
Use abs($num) first so each line stays positive without a leading minus.
Yes. Convert to string and print substr($s, 0, $len) — see Example 3.
intdiv(120, 10) becomes 12 immediately — trailing zeros drop like any other last digit.
Yes. Use fgets(STDIN) with is_numeric() and the same while-loop — see Example 2.
Here each line uses echo $num . PHP_EOL — one call prints the value and ends the line. Nested-row patterns often echo digits first, then PHP_EOL after the inner loop.
O(d) where d is the number of digits — one loop iteration per digit.
Use trim(fgets(STDIN)) and is_numeric($input) before casting to int.

Did you know?

Integer 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) in PHP.

Next: Progressive Reverse-Build

Continue with a pattern that builds a number by reversing digits.

Program 61 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful