Calculate Sum of Digits using php.


This is small PHP tutorial on how calculate sum of digits using php? Mostly this type of program or logic we have to need where we want to some effect on alternatively.

$number = 15; // assign a number to a variable. like 15, 25,115
$sum = 0; // assign a variable in which you want to store sum digits.

//initialize a while loop. Until the value of the number reaches 0.
while($number>0) {
    $m = $number%10;
    $sum = $sum+$m;
    $number = $number/10;
}
echo $sum;

Output: 6