Get the sum of digits in PHP

后端 未结 13 2805
攒了一身酷
攒了一身酷 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:28

    One way of getting sum of digit however this is a slowest route.

    $n=123;
    while(($n=$n-9)>9);
    echo "n: $n";
    

提交回复
热议问题