How to Check if All Digits of Given Number are Same in PHP ?
Last Updated :
16 Jul, 2024
Given a number, our task is to check if all the digits of a given number are the same. This can be useful in various scenarios, such as validating user input or processing numerical data. In this article, we will explore different approaches to check if a given number's digits are the same in PHP.
Examples:
Input: 1111
Output: All digits of 1111 are same
Input: 12345
Output: All digits of 12345 are not same
String Manipulation to Check if All Digits of Given Number are Same
One way to check if all digits are the same in a given integer is by first converting the number to a string. Then compare each characters of this string with the first one and return true if all the characters are same, otherwise false.
Example: Implementation of checking if all the digits of the given number are same in PHP using string concatenation.
PHP
<?php
// To check if all digits of given number are same
function areDigitsSame($number) {
$numberStr = (string)$number;
$firstDigit = $numberStr[0];
for ($i = 1; $i < strlen($numberStr); $i++) {
if ($numberStr[$i] != $firstDigit) {
return false;
}
}
return true;
}
// Given Number
$number = 1111;
if (areDigitsSame($number)) {
echo "All digits of $number are same.";
} else {
echo "All digits of $number are not same.";
}
?>
OutputAll digits of 1111 are same.
Time Complexity: O(d), where d is the number of digits in the given number.
Auxiliary Space: O(1)
Arithmetic Operations to Check if All Digits of Given Number are Same
Another approach to check if all digits are the same in a given integer is by comparing each digit directly by using arithmetic operations, without converting the number to a string.
Example: Implementation of checking if all the digits of the given number are same in PHP using arithmetic operations.
PHP
<?php
// To check if all digits of given number are same
function areDigitsSame($number) {
$lastDigit = $number % 10;
while ($number > 0) {
$currentDigit = $number % 10;
if ($currentDigit != $lastDigit) {
return false;
}
$number = (int)($number / 10);
}
return true;
}
// Given Data
$number = 2222;
if (areDigitsSame($number)) {
echo "All digits of $number are same.";
} else {
echo "All digits of $number are not same.";
}
?>
OutputAll digits of 2222 are same.
Time Complexity: O(d), where d is the number of digits in the given number.
Auxiliary Space: O(1)
String Functions to Check if All Digits of Given Number are Same
PHP provides various methods to manipulate a string. We can use them to check if all the digits of a given string are same or not. First convert the number to a string and split it into an array of digits using str_split()
function
. Then use array_unique()
function
to remove duplicate digits and checks if the resulting array has only one element. If it does, all digits are the same; otherwise, they are not.
Example: Implementation of checking if all the digits of the given number are same in PHP using in-built string functions.
PHP
<?php
// To check if all digits of given number are same
function areDigitsSame($number) {
$numberStr = (string)$number;
return count(array_unique(str_split($numberStr))) === 1;
}
// Given Data
$number = 3333;
if (areDigitsSame($number)) {
echo "All digits of $number are same.";
} else {
echo "All digits of $number are not same.";
}
?>
OutputAll digits of 3333 are same.
Time Complexity: O(d), where d is the number of digits in the given number.
Auxiliary Space: O(1)
Using a Frequency Array
This approach involves creating an array to count the frequency of each digit (0-9). If only one digit has a non-zero frequency, then all digits in the number are the same.
Example: Implementation of checking if all the digits of the given number are the same in PHP using a frequency array.
PHP
<?php
// To check if all digits of given number are same
function areDigitsSame($number) {
$frequency = array_fill(0, 10, 0);
while ($number > 0) {
$digit = $number % 10;
$frequency[$digit]++;
$number = (int)($number / 10);
}
$nonZeroCount = 0;
foreach ($frequency as $count) {
if ($count > 0) {
$nonZeroCount++;
}
}
return $nonZeroCount === 1;
}
// Given Data
$number = 4444;
if (areDigitsSame($number)) {
echo "All digits of $number are same.";
} else {
echo "All digits of $number are not same.";
}
?>
OutputAll digits of 4444 are same.
Using Regular Expressions
Another approach to check if all digits in a given number are the same is by using regular expressions. This method involves converting the number to a string and then using a regular expression pattern to determine if all characters in the string are identical.
Example: The following example demonstrates how to check if all the digits of a given number are the same using regular expressions in PHP.
PHP
<?php
function areAllDigitsSame($number) {
// Convert the number to a string
$numberStr = strval($number);
// Use a regular expression to check if all characters are the same
return preg_match('/^(\d)\1*$/', $numberStr) === 1;
}
$number1 = 1111;
$number2 = 12345;
if (areAllDigitsSame($number1)) {
echo "All digits of $number1 are same\n";
} else {
echo "All digits of $number1 are not same\n";
}
if (areAllDigitsSame($number2)) {
echo "All digits of $number2 are same\n";
} else {
echo "All digits of $number2 are not same\n";
}
?>
OutputAll digits of 1111 are same
All digits of 12345 are not same
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
PHP check if a number is Even or Odd
Checking if a number is even or odd involves determining whether the number is divisible by 2. An even number has no remainder when divided by 2, while an odd number has a remainder of 1. This can be done using various methods like modulo or bit manipulation.Examples :Input : 42 Output : Even Explan
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 Check if a Variable Is a Number in Bash
A number contains a combination of digits from 0-9. All the variables in Bash are treated as character strings but the arithmetic operations and comparisons are allowed even when it is stored in the string format. But before performing any arithmetic operations, we need to check if the stored value
5 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 check an element is exists in array or not in PHP ?
An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP
2 min read
PHP | Check if a number is prime
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The smallest prime number is 2, which is the only even prime number. All other even numbers are not prime because they are divisible by 2.2 is prime because it can only be divided by 1 and 2.3 is
3 min read
Check Whether a Given Number is Ugly Number or Not in PHP
Given an integer N, the task is to find out whether the given number is an Ugly number or not. Ugly numbers are numbers whose only prime factors are 2, 3 or 5.Examples: Input: N = 14 Output: No Explanation: 14 is not ugly since it includes another prime factor 7.Input: N = 6 Output: Yes Explanation:
3 min read