Perfect Palindrome Program In PHP
In this post, you will learn different ways to check whether a number and a string are a palindrome or not using the PHP programming language.
A palindrome is a number, string, or sequence that remains the same when it is reversed. A palindrome sequence can be a combination of digits or numbers, but it looks the same going forward and backward. It is generally used where recreational mathematics is applied, like puzzles and games. To check whether a number or string is a palindrome or not, simply reverse it and compare it with the original number or string. If both are the same, then the number or string is a palindrome, otherwise not.
These are different programming ways in PHP to check for palindrome numbers and strings-
121, 1991, 1239321, MAAM, DAD, WOW
When we reverse the above numbers or strings, they will remain the same. So, all the above are palindrome.
Check Palindrome using While loop
In the given PHP code, we have used the while loop to check the palindromes. For this, we defined a function 'Check_Palindrome()' and passed an array into it as an argument. We reversed the array element using the while loop and compared it with the original element. If equal, it means the string or number is a palindrome.
<?php
function Check_Palindrome($arr){
$temp = $arr;
$rev = 0;
while (floor($temp)) {
$d = $temp % 10;
$rev = $rev * 10 + $d;
$temp = $temp/10;
}
if ($rev == $arr){
return 1;
}
else{
return 0;
}
}
//Check Palindrome
$palindrome = array(121, 1991, 1239, 'MAAM', 'SUPER', 'DAD', 'WOW');
foreach($palindrome as $p){
if(Check_Palindrome($p)){
echo $p." is a Palindrome.<br/>";
}
else {
echo $p." is not a Palindrome.<br/>";
}
}
?>
Output of the above code-
121 is a Palindrome.
1991 is a Palindrome.
1239 is not a Palindrome.
MAAM is a Palindrome.
SUPER is not a Palindrome.
DAD is a Palindrome.
WOW is a Palindrome.
Check Palindrome using PHP strrev()
PHP provides the strrev() built-in function to reverse a given string. This function accepts both string and numbers as the input string. It always returns the reversed form of the given string. It helps to check whether a string is palindrome or not. In the given PHP program, we have used an user defined function and passed an array as an argument. We applied the strrev() function on each element of the array and checked whether the original element and the reversed element are equal or not. If equal, it means the string or number is a palindrome.
<?php
function Check_Palindrome($arr){
if (strrev($arr) == $arr){
return true;
}
else{
return false;
}
}
//Check Palindrome using strrev()
$palindrome = array(121, 1991, 1239, 'MAAM', 'SUPER', 'DAD', 'WOW');
foreach($palindrome as $p){
if(Check_Palindrome($p)){
echo $p." is a Palindrome.<br/>";
}
else {
echo $p." is not a Palindrome.<br/>";
}
}
?>
Output of the above code-
121 is a Palindrome.
1991 is a Palindrome.
1239 is not a Palindrome.
MAAM is a Palindrome.
SUPER is not a Palindrome.
DAD is a Palindrome.
WOW is a Palindrome.
Recursive Function to check Palindrome
A recursion function is a function that is called by itself. In the given code, we check whether the given string is a palindrome or not using a recursive way. In the given PHP code, we call the function 'recursive_func()' from the main body. If the length of the string is 1, return True. Otherwise, we compare the first and last characters and check them. If both characters are the same, then apply recursion 'recursive_func()' to the remaining sub-string, otherwise it prints 'Not a Palindrome'.
<?php
// Recursive way to check Palindrome
function recursive_func($string){
// End of recursive process
if ((strlen($string) == 1) || (strlen($string) == 0)){
echo "Palindrome";
}
else{
// compare the first character with last
if (substr($string,0,1) == substr($string,(strlen($string) - 1),1)){
// Call function to check the next letter
return recursive_func(substr($string,1,strlen($string) -2));
}
else {
echo "Not a Palindrome";
}
}
}
$string = "RACECAR";
recursive_func($string);
?>
Related Articles
Insert image in database using PHPHow to generate QR Code in PHP
Fibonacci Series Program in PHP
PHP remove last character from string
PHP create image from text and save
PHP script to read email inbox
How to store Emoji character in MySQL using PHP
How to display PDF file in PHP from database
jQuery loop over JSON result after AJAX Success
Dynamically Add/Delete HTML Table Rows Using Javascript
Submit a form data without page refresh using PHP, Ajax and Javascript
PHP Server Side Form Validation
How to add google reCAPTCHA v2 in registration form using PHP
Preventing Cross Site Request Forgeries(CSRF) in PHP
Recover forgot password using PHP and MySQL
Complete HTML Form Validation in PHP
How to send emojis in email subject and body using PHP
PHP7.3 New Features, Functions and Deprecated Functions