Q:

Write a PHP program to compute the sum of the digits of a number

0

Write a PHP program to compute the sum of the digits of a number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function sum_of_digits($nums) {
    $digits_sum = 0;
      for ($i = 0; $i < strlen($nums); $i++) {
             $digits_sum += $nums[$i];
               }
      return $digits_sum;
}
echo sum_of_digits("12345")."\n";
echo sum_of_digits("9999")."\n";
?>

Sample Output:

15                                                          
36

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now