Get the sum of digits in PHP

后端 未结 13 2682
攒了一身酷
攒了一身酷 2020-11-29 10:45

How do I find the sum of all the digits in a number in PHP?

相关标签:
13条回答
  • 2020-11-29 11:47
        <?php
    echo"----Sum of digit using php----";
    echo"<br/ >";
    $num=98765;
    $sum=0;
    $rem=0;
    for($i=0;$i<=$num;$i++)
    {
    $rem=$num%10;
    $sum=$sum+$rem;
    $num=$num/10;
    }
    echo "The sum of digit 98765 is ".$sum;
    ?>
    -----------------Output-------------
    ----Sum of digit using php----
    The sum of digit 98765 is 35
    
    0 讨论(0)
提交回复
热议问题