Updated May 20, 2023
Definition of Palindrome
Before understanding, Palindrome in PHP we will first study Palindrome. Palindrome means there is no change in the original and the reverse of the string. Meaning that the Palindrome of a number is the same as the original even after the reverse of the string. This can be applied to numbers also.
For Example:
Input: 12321
Reverse: 12321
Input: Civic
Reverse: Civic
To know whether a string or a number is Palindrome or not, we will use the built-in function in PHP.
Palindrome Logic
The logic behind getting a palindrome is as per the following:
- Get an input number or string.
- Get the reverse of the input number or string using the built-in function.
- Compare both the numbers or strings – the input and the reverse number or string.
- If the input and the reverse are found to be equal, it means that the number or string is a palindrome.
How to Check Palindrome in PHP?
To check the Palindrome of a number, we will use the built-in function called strrev()
About the strrev() function in PHP: This function accepts both string and numbers as the input string. It performs reverse on the input string but does not change the given string. It always returns the reversed form of the given string.
Example #1
In the following program, we have an input string, MADAM; on this string strrev() function is applied. After applying the function, the result returns the exact string MADAM; then, a condition is checked to determine whether the input and the reversed string are equal or not.
Code:
<?php
// example to get the palindrome of a string using built in function
//input string is MADAM
$input = "MADAM";
echo '<br> Input String '. $input;
//reverse of input string - MADAM - using strrev
$reverse = strrev($input);
echo '<br> Ouput String '. $reverse;
//condition to check if the input and the reverse of the string is equal or not
if($input == $reverse) {
echo '<br> '.$input.' is a palindrome';
}
else {
echo '<br>'.$input.' is not a palindrome';
}
?>
Output:
Example #2
As seen in the above program, the input string is a palindrome. Let us now apply the same strrev function on a number to check whether the input number is a palindrome or not.
Code:
<?php
//example to get the palindrome of a number using built in function
// input string is 1234321
$input = 1234321;
echo '<br>'.'Input string '. $input;
//reverse of input string using strrev
$reverse = strrev($input);
echo '<br>'.'Reverse string '.$reverse;
//condition to check if the input and the reverse of the string is equal or not
if($input == $reverse) {
echo '<br>'.$input.' is a palindrome';
}
else {
echo '<br>'.$input.' is not a palindrome';
}
?>
Output:
Example #3
In the below program, we have used the strrev() built-in function defined within a different function named Palindrome_Function. So when this function is called to reverse a string, it performs reverse on the input string using the strrev() function. You can complete the same program in the following way.
Code:
<?php
//example to get the palindrome of a number using built in function
function Palindrome_Function($input) {
// applying strrev() function to input string
$reverse = strrev($input);
//condition to check if reverse and input strings are same or not
if($reverse == $input) {
return true;
}
else
{
return false;
}
}
$input = 1995991;
//calling the reverse function
$result = Palindrome_Function($input);
if($result == TRUE) {
echo $input.' is palindrome';
}
else
{
echo $input.' is not palindrome';
}
?>
Output:
Example #4
In the below program, we will input a number that is not a palindrome number and see the result.
Code:
<?php
//example to get the palindrome of a number using built in function
function Palindrome_Function($input) {
$reverse = strrev($input);
if($reverse == $input) {
return true;
}
else
{
return false;
}
}
$input = 13241;
$result = Palindrome_Function($input);
if($result == TRUE) {
echo $input.' is palindrome';
}
else
{
echo $input.' is not palindrome';
}
?>
Output:
Example #5
Following is the program wherein we have a form containing an input text box. On entering a number and submitting the form, we have the result, which tells us whether the input number is a palindrome or not.
Code:
<html>
<head>
<title>Palindrome Program</title>
</head>
<body>
<form method="post" action="program1.php">
<input type="text" name="number" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['number'])) {
$input = $_POST['number'];
$reverse = strrev($input);
if($reverse == $input) {
echo $input . 'is a palindrome';
}
else{
echo $input. 'is not a palindrome';
}
}
?>
</body>
</html>
Output:
In the below program, we have the following steps to get the reverse of a number without using the strrev() function.
We will be using the while loop here:
- Get an Input number
- Divide the number by 10 to get the remainder
- Add the remainder to the new variable, which is multiplied by 10
- Divide the number by 10.
Code:
<?php
//example to check if number is palindrome or not without using function only for numbers
//defining the palindrome function
function Palindrome_Function($input) {
$number = $input;
$sum = 0;
//using while loop to get the reverse of the input number
while(floor($number))
{
$remainder = $number % 10;
$sum = $sum * 10 + $remainder;
$number = $number / 10;
}
if($sum == $input) {
return true;
}
else {
return false;
}
}
//passing the input number
$input_number = 1546451;
//calling the Palindrome_Function
$result = Palindrome_Function($input_number);
//check if the input is equal to output of palindrome_function
if($result){
echo "<br>"." $input_number is a Palindrome"; //if equal show $input is palindrome number
} else {
echo "<br>"."$input_number is not a Palindrome"; //if not equal show $input is not a palindrome number
}
?>
Output:
Conclusion – Palindrome in PHP
This article explains what a palindrome is, how we find whether a number is a palindrome or not, and how to know whether the input string is a palindrome or not. I hope this article was helpful.
Recommended Articles
This is a guide to Palindrome in PHP. Here we discuss the introduction and examples of checking Palindrome with various examples and Palindrome logic. You can also go through our other related articles to learn more-