Sum of Digits in PHP

Programming.jpg

The sum of the digits of variety is named its digit sum. For instance , the digit sum of 15 is 6 because 1 + 5 = 6.

12987 = 1 + 2 + 9 + 8 + 7  
12987 = 27 

Sum of Digits in PHP

<?php  
$num = 12987;  
$sum=0; $rem=0;  
  for ($i =0; $i<=strlen($num);$i++)  
 {  
  $rem=$num%10;  
   $sum = $sum + $rem;  
   $num=$num/10;  
  }  
 echo "Sum of digits 12987 is $sum";  
 ?>