In this post, we will learn how to check number/string is palindrome or not in PHP. Now you might be thinking, what is palindrome, so let's know about palindrome first.


How To Check Number/String is Palindrome in PHP ?


What is Palindrome ?

Palindrome means that when a number or string are reversed, yet their order remains the same, then that number or string is called Palindrome.

To understand this, you can see these examples given below:

Number Palindrome:

Input: 121
Reverse: 121

String Palindrome:

Input: WOW
Reverse: WOW


How To Check Number/String is Palindrome in PHP ?

To check number/string is palindrome or not in PHP, we will use strrev() function. This is a built in PHP function. This function take both number or string and give reverse number or string as output. Then we match the original number/string with the reverse number/string. If  it matches so this number/string is a palindrome. 

To understand this better, let us see these examples given below:

For String:

<?php
$input = "abcba";
$rev = strrev($input);
if($input==$rev) {
echo "$input is Palindrome";
} else {
echo "$input is not Palindrome";
}
?> code-box

Output:
abcba is Palindrome


For Number:

<?php
$input = 1221;
$rev = strrev($input);
if($input==$rev) {
echo "$input is Palindrome";
} else {
echo "$input is not Palindrome";
}
?> code-box

Output:
1221 is Palindrome


Check Palindrome With Input Text Box

To check palindrome with input text box, we follow the following code.

<html>
<head>
<title>Check Palindrome in PHP</title>
</head>
<body>
<form method="POST">
<input type="text" name="input" required />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])) {
$input = $_POST['input'];
$rev = strrev($input);
if($rev == $input) {
echo '$input is Palindrome';
}
else{
echo '$input is not Palindrome';
}
}
?>
</body>
</html> code-box


Use Custom Function Instead of strrev() function

Instead of strrev() function, we can also use custom function to reverse any string. This is only for string. For this, we follow the following code.

<?php
$input = "abcba";
$rev = getRevStr($input);
if($input==$rev) {
echo "$input is Palindrome";
} else {
echo "$input is not Palindrome";
}

// Custom function for reverse only string
function getRevStr($str) {
$revstr = "";
$count = strlen($str) - 1;
for($i = $count; $i >= 0; $i--) {
$revstr.= $str[$i];
}
return $revstr;
}?> code-box

Output:
abcba is Palindrome


Use while loop Instead of strrev() function

Instead of strrev() function, we can also use while loop to reverse any number. This is only for number. For this, we follow the following code.

<?php
$input = 1221;
$rev = getRevNum($input);
if($input==$rev) {
echo "$input is Palindrome";
} else {
echo "$input is not Palindrome";
}

// Custom function for reverse only number
function getRevNum($num) {
$number = $num;
$sum = 0;
$revnum = "";
while(floor($number))
{
$remainder = $number % 10;
$sum = $sum * 10 + $remainder;
$number = $number / 10;
$revnum = $sum;
}
return $revnum ;

}?> code-box

Output:
1221 is Palindrome


Summary

In this post we learn how to check number/string is palindrome in PHP. You try to make it yourself. I hope you make. If you face any problem, then comment and tell us. Keep visiting the blog to read more similar posts.

Post a Comment

Previous Post Next Post