How to find Sum the Digits of a given Number in PHP ? Last Updated : 25 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 extract digits one by oneIn this approach, we are using a for loop to iterate over each character in the digit. First, we will convert the number to a string to easily access each digit. Then we will iterate over each character in the string. Convert each character back to a number and add it to a running sum. Example: The below example uses for loop to to find the sum of the digits of a given number in PHP. PHP <?php // function to calculate sum of digits function sumDigits($number) { $sum = 0; // converting number to string to access digits easily $numberStr = (string)$number; for ($i = 0; $i < strlen($numberStr); $i++) { $digit = (int)$numberStr[$i]; $sum += $digit; } return $sum; } $number = 12345; echo "Sum of digits: " . sumDigits($number); ?> OutputSum of digits: 15Using mathematical operations to extract digitsIn this approach, we are using mathematical operations to calculate sum of digits of numbers. First we use modulus operator (%) to extract the last digit of the number then add that extracted digit to a running sum. Then divide the number by 10 to remove the last digit. We will repeat the above steps until the number becomes 0. Example: This example uses mathematical operations to find the sum of the digits of a given number in PHP. PHP <?php function sumDigits($number) { $sum = 0; while ($number != 0) { $digit = $number % 10; $sum += $digit; $number = (int)($number / 10); } return $sum; } $number = 76534; echo "Sum of digits: " . sumDigits($number); ?> OutputSum of digits: 25 Comment More infoAdvertise with us Next Article How to convert a String into Number in PHP ? Y yuvrajghule281 Follow Improve Article Tags : PHP Similar Reads How to Convert a Given Number to Words in PHP? Given an integer N, your task is to convert the given number into words using PHP.Examples:Input: N = 958237764Output: Nine Hundred Fifty Eight Million Two Hundred Thirty Seven Thousand Seven Hundred Sixty FourInput: N = 5000Output: Five ThousandBelow are the approaches to convert a given number to 5 min read PHP | Sum of digits of a number 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 ext 1 min read How to convert a String into Number in PHP ? Strings in PHP can be converted to numbers (float/ int/ double) very easily. In most use cases, it won't be required since PHP does implicit type conversion. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations.There are 4 min read How to extract Numbers From a String in PHP ? Extracting numbers from a string involves identifying and isolating numerical values embedded within a text. This process can be done using programming techniques, such as regular expressions, to filter out and retrieve only the digits from the string, ignoring all other characters.Here we have some 3 min read How to Convert Number to Character Array in PHP ? Given a number, the task is to convert numbers to character arrays in PHP. It is a common operation when you need to manipulate or access individual digits of a number. This can be particularly useful in situations where you need to perform operations on the digits of a number, such as digital root 3 min read How to format Phone Numbers in PHP ? In this article, we will learn how to format phone numbers using PHP. When we need to store a phone number then we store the formatting phone number. Using PHP, we can easily format phone numbers. Approach: In this article, we will format the phone number using the preg_match() method. We can use th 1 min read Like