PHP Program to find Sum of Digits

Last Updated:

Find Sum of Digits

In the following example, we will add all the digits in the given number (12345) for just one time.

For example, 12345 = 1 + 2 + 3 + 4 + 5
i.e., 12345 = 15

Example

PHP Compiler
<?php $num = 12; $copyNum = $num; $total = 0; // Add Each digit from last digit while($copyNum != 0) { $total += $copyNum % 10; $copyNum = floor($copyNum / 10); } // result echo "Sum of digit of $num: $total"; ?>

Share this Page

Meet the Author