Truncate Last Digit While Loop Pattern in PHP

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
While Loop + Arithmetic

What You’ll Learn

How to print a digit-ladder by repeatedly removing the last digit of an integer with floor($num / 10) inside a while loop.

This pattern reinforces loop conditions, integer truncation, and why floor (or intdiv) matters in PHP.

⭐ Pattern Output

Starting from 86523, each line drops the last digit until the value would reach zero (zero itself is not printed):

Output
86523
8652
865
86
8
1

Complete PHP Program

Print the current number, then replace it with floor($num / 10) until it becomes 0.

PHP
<?php
$num = 86523;
while ($num != 0) {
    echo $num . PHP_EOL;
    $num = (int) floor($num / 10);
}

🧠 How It Works

1

Initialize

$num = 86523 sets the starting value (any positive integer works similarly).

Setup
2

Loop while non-zero

while ($num != 0) keeps going until every digit has been stripped.

Condition
3

Print then truncate

Echo the current value, then set $num = floor($num / 10) to drop the last digit.

Body
4

Stop at zero

After printing 8, $num becomes 0 and the loop exits, so trailing zero is not shown.

Termination
=

Digit ladder

Iterations equal the number of digits in the starting value, so complexity is O(d) for digit count d.

2

Variation — User Input (CLI) Version

Reads a positive integer from stdin and prints the same truncation ladder (rejects zero or negative input):

PHP
<?php
echo "Enter a positive integer: ";
$num = (int) trim(fgets(STDIN));

if ($num <= 0) {
    echo "Enter a positive integer greater than 0" . PHP_EOL;
    exit;
}

while ($num != 0) {
    echo $num . PHP_EOL;
    $num = (int) floor($num / 10);
}

💡 Tips for Enhancement

Try These

  • Use intdiv($num, 10) for non-negative integers
  • Print leading zeros on each line for fixed-width display
  • Collect lines into an array and implode for testing
  • After the loop, optionally echo a final 0 if you want it shown
  • Log iteration count to relate output length to digit count

Avoid

  • Infinite loops: ensure $num strictly moves toward 0
  • Assuming plain / always returns integers in PHP
  • Using this exact truncation for negative numbers without adjusting rules
  • Using while (true) without a clear break condition

Key Takeaways

1

Truncating the last digit is floor(n / 10) (or intdiv(n, 10)) for non-negative n.

2

The loop condition != 0 controls whether a final zero line appears.

3

Iteration count matches the number of digits in the original number.

4

Same idea underlies base conversion and digit-extraction algorithms.

❓ Frequently Asked Questions

You risk float precision issues for very large integers. floor plus (int) cast, or intdiv, keeps behavior predictable.
Only one line prints (that digit), then $num becomes 0 and the loop ends.
Same math; a while loop highlights the stopping condition on zero and avoids knowing digit length in advance.
O(d) iterations for d decimal digits of the starting value.

Explore More PHP Number Patterns!

Practice digit manipulation, then combine it with arrays and string reversal for richer patterns.

All Number Patterns →
Did you know?

Extracting digits with division and modulo (% 10) is the standard complement to this truncation-from-the-right trick.

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