PHP | Sum of digits of a number Last Updated : 09 Mar, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report This is a simple PHP program where we need to calculate the sum of all digits of a number. Examples: Input : 711 Output : 9 Input : 14785 Output : 25 In this program, we will try to accept a number in the form of a string and then iterate through the length of the string. While iterating we will extract the digit from each position and then add them to the previously extracted digit, thus getting the sum. PHP <?php // PHP program to calculate the sum of digits function sum($num) { $sum = 0; for ($i = 0; $i < strlen($num); $i++){ $sum += $num[$i]; } return $sum; } // Driver Code $num = "711"; echo sum($num); ?> Output: 9 Comment More infoAdvertise with us Next Article PHP Program to Print ASCII Value of all Digits of a Given Number C chinmoy lenka Follow Improve Article Tags : Misc Web Technologies PHP PHP-basics Practice Tags : Misc Similar Reads Sum of digits of a number in PL/ SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a number and task is to find the sum of digits of 1 min read Sum of digit of a number using recursion Given a number, we need to find sum of its digits using recursion.Examples: Input: 12345Output: 15Explanation: Sum of digits â 1 + 2 + 3 + 4 + 5 = 15Input: 45632Output: 20 Approach:To understand the algorithm, consider the number 12345 and refer to the illustration below..Extract the last digit: 123 3 min read How to find Sum the Digits of a given Number in PHP ? We will explore how to find the sum of the digits of a given number in PHP. This task involves extracting each digit from the number and adding them together to get the final sum. Table of Content Using a loop to extract digits one by oneUsing mathematical operations to extract digitsUsing a loop to 2 min read n-th number whose sum of digits is ten Given an integer value n, find out the n-th positive integer whose sum is 10. Examples: Input: n = 2 Output: 28 The first number with sum of digits as 10 is 19. Second number is 28. Input: 15 Output: 154 Method 1 (Simple): We traverse through all numbers. For every number, we find the sum of digits. 8 min read PHP Program to Print ASCII Value of all Digits of a Given Number Given a number, the task is to print ASCII value of all digits of a given number in PHP. ASCII values are often used to represent characters and digits. Sometimes, it's necessary to find the ASCII value of each digit in a given number, especially when dealing with character encoding or data manipula 3 min read PHP | Check if a number is Perfect number A perfect number is a number if it is equal to the sum of its factors,that is original number is equal to sum of all its factors excluding the number itself. We have already discuss how to check if a number is perfect or not in this article. In this article we will discuss about how to do the same i 2 min read Like