PHP Palindrome Number
In this article, we will learn about Palindrome Numbers and how to write PHP code to check whether a given number is a palindrome number or not.
What is a Palindrome Number?
A palindrome number is a number that remains the same when its digits are reversed. For example, 121, 1221, 12321, 454, etc., are all palindrome numbers.
Syntax
The syntax for checking if a given number is a palindrome number in PHP is:
function isPalindrome($num){
$temp = $num;
$new = 0;
while (floor($temp)) {
$d = $temp % 10;
$new = $new * 10 + $d;
$temp = $temp/10;
}
if ($new == $num){
return "Palindrome number";
}
else{
return "Not a Palindrome number";
}
}
Example
Let's take an example to understand how to use the above PHP code to determine if a given number is a palindrome number or not.
Suppose we have a number $num = 12321
and we want to check if it is a palindrome number or not.
We will call the function isPalindrome($num)
that we defined above and it will return the output Palindrome number
.
Output
If the input number is a palindrome number, the output will be Palindrome number
, and if it is not a palindrome number, the output will be Not a Palindrome number
.
Explanation
In PHP, we can check a given number's Palindrome by reversing its digits and then comparing it with the original number. If both the numbers are equal, then the given number is Palindrome.
In the PHP code given above, we first create a function called isPalindrome
that takes a number as a parameter. We then create two variables $temp
and $new
.
We assign the value of the input parameter to the variable $temp
. We use the while
loop to find the reverse of the input number and assign it to the variable $new
.
In the while
loop, we first find the last digit of the $temp
variable using the modulus symbol %
and store it in the variable $d
. We then multiply the value of $new
by 10
and add the value of $d
to it. After that, we divide the value of $temp
by 10
and assign the quotient to the variable $temp
.
We continue executing the while
loop until $temp
becomes 0
or less than 1
. After that, we compare the value of $new
with the input number $num
. If they are equal, we return "Palindrome number"
, and if they are not equal, we return "Not a Palindrome number"
.
Overall, this program successfully checks whether the input number is a Palindrome number or not.
Use
This code can be used by PHP developers to check if a given number is Palindrome or not. It will return the string "Palindrome number"
if the input number is a palindrome and "Not a Palindrome number"
if the input number is not a palindrome.
Important Points
- A palindrome number is a number that remains the same when its digits are reversed.
- We can use a PHP while loop and some mathematical operations to check if a given number is Palindrome or not.
- The PHP function
isPalindrome()
takes a number as input and returns"Palindrome number"
if the input number is Palindrome and"Not a Palindrome number"
if the input number is not Palindrome.
Summary
In this article, we discussed how to check whether a given number is a palindrome number or not using PHP code. We explained the syntax, example, output, explanation, use, and important points related to palindrome numbers. By using this code, PHP developers can easily determine if a given number is a palindrome number or not.