How to Increment by 1 to all the Digits of a given Integer in PHP ?
Last Updated :
09 Jul, 2024
In PHP, incrementing all digits of an integer by 1 involves breaking the number into individual digits, adding 1 to each, and reconstructing the integer. This can be accomplished through array manipulation, arithmetic operations, or string conversion methods. Suppose, if the given number is 123 then the desired output is 234.
Examples:
Input: 12345
Output: 23456
Input: 110102
Output: 221213
Using String Conversion
Generate a string which will be of the same length as the input integer and will contain only 1's in it. Then we will convert this string into an integer. Then, finally add this integer to the original integer to get the desired result.
Example: Illustration of incrementing by 1 to all the digits of a given integer in PHP using string conversion method.
PHP
<?php
// Declaring the number
$number = 110102;
echo "Original Number: $number \n";
// Number of digits in the number
$len = strlen((string) $number);
// Generating the addition string
$add = str_repeat("1", $len);
// Converting it to an integer
$str_num = (int) $add;
// Adding them and displaying the result
$result = $number + $str_num;
// Print the result
echo "Incremented Number: $result";
?>
OutputOriginal Number: 110102
Incremented Number: 221213
Using Arithmetic Operations
Take an integer with value 1. Then we will perform arithmetic operations on it to get an integer of only 1s which will be of same length as that or original integer. Then, finally we will add this integer to original integer to get the desired result.
Example: Illustration of incrementing by 1 to all the digits of a given integer in PHP using arithmetic operations.
PHP
<?php
// Declaring the number
$number = 1920;
echo "Original Number: $number \n";
// Number of digits in the number
$len = strlen((string) $number);
// Declaring another variable with value 1
$add = 1;
for ($i = 0; $i < $len; $i++) {
// Adding variable $add and $number
$number = $number + $add;
// Multiplying the value of $add with 10
$add = $add * 10;
}
// Printing the result
echo "Incremented Number: $number";
?>
OutputOriginal Number: 1920
Incremented Number: 3031
Using Array Manipulation
Create an array with value of all elements equals to 1. Then, merge all elements of array together into a string and then convert this string into a number. Finally, add this number to the original given integer to get the result.
Example: Illustration of incrementing by 1 to all the digits of a given integer in PHP using array manipulation method.
PHP
<?php
// Declaring the number
$number = 2024;
echo "Original Number: $number \n";
// Converting the number to an array of digits
$num_arr = str_split(strval($number));
// Counting the number of elements n numberArray
$len = count($num_arr);
// Creating an array of 1s with the length = len
$ones_arr = array_fill(0, $len, 1);
// Joining all array elements to form a string of 1s
// with length len and then convert it to an integer
$str_num = (int) implode("", $ones_arr);
// Adding them and displaying the result
$result = $number + $str_num;
// Print the result
echo "Incremented Number: $result";
?>
OutputOriginal Number: 2024
Incremented Number: 3135
Using Direct Digit Manipulation
In this method, we break down the integer into its individual digits, increment each digit by 1, handle any carry that might occur due to digits reaching 10, and then reconstruct the integer.
Example: Illustration of incrementing each digit by 1 to all the digits of a given integer in PHP using direct digit manipulation.
PHP
<?php
// Function to increment each digit by 1
function incrementDigits($number) {
// Convert the number to an array of digits
$num_arr = str_split(strval($number));
// Increment each digit by 1
for ($i = 0; $i < count($num_arr); $i++) {
$num_arr[$i] = ($num_arr[$i] + 1) % 10;
}
// Join the array back into a string
$incremented_number = implode("", $num_arr);
// Convert the string back to an integer
return (int) $incremented_number;
}
// Declaring the number
$number = 1920;
echo "Original Number: $number \n";
// Incrementing each digit
$result = incrementDigits($number);
// Print the result
echo "Incremented Number: $result";
?>
OutputOriginal Number: 1920
Incremented Number: 2031
Similar Reads
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
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
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
How to convert an Integer Into a String in PHP ?
The PHP strval() function is used to convert an Integer Into a String in PHP. There are many other methods to convert an integer into a string. In this article, we will learn many methods.Table of ContentUsing strval() function.Using Inline variable parsing.Using Explicit Casting.Using sprintf() Fun
3 min read
PHP Program to Count set bits in an integer
Write an efficient program to count number of 1s in binary representation of an integer. Examples : Input : n = 6 Output : 2 Binary representation of 6 is 110 and has 2 set bits Input : n = 13 Output : 3 Binary representation of 11 is 1101 and has 3 set bits Recommended: Please solve it on âPRACTICE
2 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 get elements in reverse order of an array in PHP ?
An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements
4 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
Write a program to display Reverse of any number in PHP ?
Write a program to reverse the digits of an integer. Examples : Input : num = 12345 Output: 54321 Input : num = 876 Output: 678 It can be achieved using Iterative way or Recursive Way Iterative Method: Algorithm: Input: num (1) Initialize rev_num = 0 (2) Loop while num > 0 (a) Multiply rev_num by
2 min read
How to repeat a string to a specific number of times in PHP ?
A string is a sequence of characters stored in PHP. The string may contain special characters or numerical values or characters. The strings may contain any number of characters and may be formed by the combination of smaller substrings. Table of ContentUsing for loopUsing str_repeat methodUsing Rec
3 min read