Get the sum of digits in PHP

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

    array_sum(str_split($number));
    

    This assumes the number is positive (or, more accurately, that the conversion of $number into a string generates only digits).

提交回复
热议问题