0
0
PhpProgramBeginner · 2 min read

PHP Program to Find Sum of Digits of a Number

You can find the sum of digits in PHP by converting the number to a string and adding each digit using a loop, like $sum = 0; foreach (str_split($num) as $digit) { $sum += (int)$digit; }.
📋

Examples

Input123
Output6
Input405
Output9
Input0
Output0
🧠

How to Think About It

To find the sum of digits, think of the number as a collection of single digits. You add each digit one by one to a total sum. You can do this by turning the number into a string and looping through each character, or by repeatedly taking the last digit using % and removing it using / until the number is zero.
📐

Algorithm

1
Get the input number.
2
Initialize a sum variable to zero.
3
While the number is greater than zero:
4
Extract the last digit using modulus 10.
5
Add the digit to the sum.
6
Remove the last digit by dividing the number by 10 and taking the integer part.
7
Return or print the sum.
💻

Code

php
<?php
$num = 123;
$sum = 0;
while ($num > 0) {
    $digit = $num % 10;
    $sum += $digit;
    $num = (int)($num / 10);
}
echo $sum;
?>
Output
6
🔍

Dry Run

Let's trace the number 123 through the code to find the sum of its digits.

1

Initial values

$num = 123, $sum = 0

2

First iteration

digit = 123 % 10 = 3; sum = 0 + 3 = 3; num = 123 / 10 = 12

3

Second iteration

digit = 12 % 10 = 2; sum = 3 + 2 = 5; num = 12 / 10 = 1

4

Third iteration

digit = 1 % 10 = 1; sum = 5 + 1 = 6; num = 1 / 10 = 0

5

Loop ends

num is now 0, loop stops; final sum = 6

Iterationnumdigitsum
112333
21225
3116
💡

Why This Works

Step 1: Extract each digit

Using % 10 gets the last digit of the number because it gives the remainder when divided by 10.

Step 2: Add digit to sum

We add the extracted digit to the running total stored in $sum.

Step 3: Remove last digit

Dividing the number by 10 and taking the integer part removes the last digit, preparing for the next loop iteration.

🔄

Alternative Approaches

String conversion and loop
php
<?php
$num = 123;
$sum = 0;
foreach (str_split((string)$num) as $digit) {
    $sum += (int)$digit;
}
echo $sum;
?>
This method is simple and easy to read but uses extra memory to convert the number to a string.
Recursive function
php
<?php
function sumDigits($num) {
    if ($num == 0) return 0;
    return ($num % 10) + sumDigits((int)($num / 10));
}
echo sumDigits(123);
?>
This uses recursion to sum digits but can be less efficient for very large numbers due to call stack overhead.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs once for each digit in the number, so time grows linearly with the number of digits.

Space Complexity

Only a few variables are used, so space is constant regardless of input size.

Which Approach is Fastest?

The modulus and division loop is fastest and uses least memory; string conversion is simpler but uses extra space; recursion is elegant but less efficient.

ApproachTimeSpaceBest For
Modulus and division loopO(n)O(1)Performance and low memory
String conversion and loopO(n)O(n)Readability and simplicity
Recursive functionO(n)O(n)Learning recursion and elegance
💡
Use modulus % 10 to get the last digit and integer division to remove it.
⚠️
Forgetting to convert the number to integer after division, which can cause an infinite loop.