Find Sum of Digits in PHP
What you’ll learn
- How to extract digits using
% 10and/ 10. - The accumulator pattern:
sum += digit. - A clean PHP solution with a simple loop and safe handling of negative input.
Overview
To find the sum of digits of a number, you repeatedly take the last digit, add it, and remove it. For example, 12345 becomes 5 (add), then 1234, then 4 (add), and so on.
Two programs
Example 1 uses a fixed number = 12345. Example 2 reads the number from the user.
Live preview
Type a number and see the digits added up as an expression (like 1 + 2 + 3).
Next step
If you repeat digit sums until one digit remains, you get the digital root (see “Condense a number”).
Live preview
Enter an integer (example: 12345 or -802) and see the sum of digits.
Algorithm
Goal: compute the sum of digits of an integer n.
Make n nonnegative
If n is negative, use n = -n (or a safe absolute-value variant).
Extract and add digits
While n > 0, do digit = n % 10, add it, then set n = n / 10.
Handle n = 0
If the input is 0, the sum of digits is 0.
📜 Pseudocode
function digitSum(n):
if n < 0: n ← -n
if n = 0: return 0
sum ← 0
while n > 0:
sum ← sum + (n mod 10)
n ← floor(n / 10)
return sumSum of digits (fixed number)
Matches the classic demo: 12345 gives 15.
<?php
function digitSum(int $number): int
{
if ($number < 0) {
$number = -$number; // ignore sign
}
$sum = 0;
while ($number != 0) {
$sum += $number % 10;
$number = (int)($number / 10);
}
return $sum;
}
$number = 12345;
$result = digitSum($number);
echo "The sum of digits of $number is: $result\n";
?>Sum of digits (user input)
Reads a number from the user and prints the sum of its digits.
<?php
function digitSum(int $number): int
{
if ($number < 0) {
$number = -$number;
}
$sum = 0;
while ($number != 0) {
$sum += $number % 10;
$number = (int)($number / 10);
}
return $sum;
}
echo "Enter a number: ";
$raw = trim(fgets(STDIN));
if (!is_numeric($raw)) {
echo "Invalid input.\n";
exit(1);
}
$number = (int)$raw;
echo "Sum of digits: " . digitSum($number) . "\n";
?>❓ FAQ
🔄 Input / output examples
| Input | Digit sum | Explanation |
|---|---|---|
12345 | 15 | 1+2+3+4+5 |
-802 | 10 | Ignore sign: 8+0+2 |
0 | 0 | Special case |
Edge cases and pitfalls
These are the common mistakes in digit problems.
Loop runs zero times
If you use while (n != 0), then for n = 0 the loop doesn’t run. That is fine, but make sure you return 0 as the digit sum.
Decide how to handle sign
Most beginner problems mean sum of digits of the absolute value. This page uses that rule.
Absolute value overflow (advanced)
For the most negative 32-bit int, -n overflows. Typical interview tasks ignore this, but if needed you can use a wider type (long long) before taking absolute value.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| Loop through digits | O(d) | O(1) |
Summary
- Core loop:
sum += n % 10, thenn /= 10. - Negatives: sum digits of the absolute value in beginner problems.
- Complexity: linear in number of digits.
Digit sum is used in quick checks like divisibility by 3 and 9: a number is divisible by 3 (or 9) exactly when its digit sum is divisible by 3 (or 9).
9 people found this page helpful
