获取PHP中的数字总和 [英] Get the sum of digits in PHP

查看:66
本文介绍了获取PHP中的数字总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在PHP中找到一个数字中所有数字的总和?

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

推荐答案

array_sum(str_split($number));

这假定数字为正(或更准确地说,将$number转换为字符串仅生成数字).

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

这篇关于获取PHP中的数字总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆