How to find the sum of a number's digits
Short answer
Turn the number into a string, split it into digits and add them:
<?php
echo array_sum(str_split((string) 2026));Output:
10Without strings
Take the last digit as the remainder of division by 10:
<?php
$number = 2026;
$sum = 0;
while ($number > 0) {
$sum += $number % 10;
$number = intdiv($number, 10);
}
echo $sum;Output:
10