The sum of digits is the total of every digit in a number after one pass. Peel digits with $n % 10 and remove them with $n = intdiv($n, 10).
Remember
digitSum(n) = sum of each digit of |n|
Example: 12345
5 + 4 + 3 + 2 + 1 = 15
One pass only — do not keep summing until one digit
(that is digital root / condense)
n
Digits
Sum
12345
1+2+3+4+5
15
-802
8+0+2
10
0
0
0
7
7
7
Approach
How to Find the Sum of Digits in PHP
Four clear steps — modulo and integer division do all the work.
Take abs($n) so negatives share the same path.
While $n !== 0, add $n % 10 to a total.
Remove the last digit with $n = intdiv($n, 10).
When $n becomes 0, return the total.
Method
Idea
Time
Best for
Modulo loop
% 10 then intdiv(..., 10)
O(digits)
Interviews, default answer
String digits
Convert to string and sum chars
O(digits)
Quick hacks (less preferred)
Repeated sum
Keep summing until one digit
O(digits)
Digital root / condense
Pseudocode
Pseudocode
function DigitSum(n):
n = abs(n)
total = 0
while n != 0:
total = total + (n mod 10)
n = n / 10
return total
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Last digit
$n % 10
Drop digit
$n = intdiv($n, 10)
Stop when
$n === 0
Negatives
abs($n) first
Zero
Digit sum is 0
vs Condense
One pass only (not digital root)
Classic check
12345 → 15
Try it
Live Preview
Enter any integer to see each peeled digit and the final digit sum.
Whole numbers only. Absolute value is used for negatives.
Result
n = 12345
Digits (right → left): 5, 4, 3, 2, 1
Sum = 15
Trace
Worked Walkthrough — n = 12345
Peel from the right, add each remainder, then drop that digit.
Calculation
n = 12345, total = 0
12345 % 10 = 5 total = 5 n = 1234
1234 % 10 = 4 total = 9 n = 123
123 % 10 = 3 total = 12 n = 12
12 % 10 = 2 total = 14 n = 1
1 % 10 = 1 total = 15 n = 0
Stop
Digit sum = 15
Code
PHP Programs
Start with a fixed number and the classic modulo loop. Then read input, and finish by tracing every peeled digit.
Example 1 — Sum of Digits (Fixed Number)
Use % 10 and intdiv until the number becomes zero.
PHP
<?php
function digitSum(int $number): int
{
$number = abs($number);
$total = 0;
while ($number !== 0) {
$total += $number % 10;
$number = intdiv($number, 10);
}
return $total;
}
$number = 12345;
echo "The sum of digits of " . $number . " is: " . digitSum($number) . "\n";
Output
The sum of digits of 12345 is: 15
How It Works
abs keeps the helper safe for negatives.
Each loop takes the last digit with $number % 10.
intdiv($number, 10) drops that digit for the next pass.
For 12345 the digits added are 5, 4, 3, 2, then 1.
When $number hits 0, return 15.
Example 2 — Sum of Digits (User Input)
Read one integer and reuse the same helper.
PHP
<?php
function digitSum(int $number): int
{
$number = abs($number);
$total = 0;
while ($number !== 0) {
$total += $number % 10;
$number = intdiv($number, 10);
}
return $total;
}
echo "Enter a number: ";
$number = (int) trim(fgets(STDIN));
echo "Sum of digits: " . digitSum($number) . "\n";
Output
Enter a number: 12345
Sum of digits: 15
How It Works
Read one integer with fgets(STDIN).
Pass it to digitSum — input and logic stay separate.
Negatives work because the helper uses abs.
Try -802 to confirm the answer is 10.
Same algorithm as Example 1 with a different data source.
Example 3 — Trace Digit Peeling for -802
Print each extracted digit so the right-to-left order is obvious.