PHP | Palindrome Check

In this article, we will learn how to check whether a number and a string is a palindrome or not in PHP. A number or string is said to be a palindrome if it remains same even after reversing the digits or letters respectively.

Examples for Palindrome Number:

Input : 1441
Output : Palindrome
Explanation: Reversing 1441 will also get 1441

Input : 12521
Output : Palindrome
Explanation remains same

Check for Palindrome number

Here we have simply used the iterative way to check for the palindrome number. Each digit is extracted in an iteration and formed the reverse number and finally, it is checked, whether it is same as the original number.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to check for Palindrome number in PHP
// Function to check for Palindrome
function Palindrome($number){  
    $temp = $number;  
    $new = 0;  
    while (floor($temp)) {  
        $d = $temp % 10;  
        $new = $new * 10 + $d;  
        $temp = $temp/10;  
    }  
    if ($new == $number){  
        return 1;  
    }
    else{
        return 0;
    }
}  
  
// Driver Code
$original = 1441; 
if (Palindrome($original)){  
    echo "Palindrome";  
else {  
echo "Not a Palindrome";  
}
  
?>  

chevron_right


Output:

Palindrome

Check for Palindrome String

Examples for Palindrome String:

Input : "MALAYALAM"
Output : Palindrome
Explanation: Reversing "MALAYALAM" will also get "MALAYALAM"

Input : "14441"
Output : Palindrome
Explanation remains same

Method 1: Using strrev()
The strrev() function is used in PHP to reverse a string. We can simply use this method to reverse the string and match it with the prevous value. If it is a match, then the string is palindrome else not.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to check for Palindrome string in PHP
// Using strrev()
function Palindrome($string){  
    if (strrev($string) == $string){  
        return 1;  
    }
    else{
        return 0;
    }
}  
  
// Driver Code
$original = "DAD"
if(Palindrome($original)){  
    echo "Palindrome";  
else {  
echo "Not a Palindrome";  
}
?>  

chevron_right


Output:

Palindrome

Method 2: Recursive way using substr()
The substr() method is used to return a part of a string, referred to as the substring. Using this method, there is a recursive way to check for Palindrome or not. In this method no new string is formed and the original string is modified in each recursive call.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to check for Palindrome string in PHP
// Recursive way using substr()
function Palindrome($string){
      
    // Base codition to end the recursive process
    if ((strlen($string) == 1) || (strlen($string) == 0)){
        echo "Palindrome";
    }
  
    else{
          
        // First character is compared with the last one
        if (substr($string,0,1) == substr($string,(strlen($string) - 1),1)){
              
            // Checked letters are discarded and passed for next call
            return Palindrome(substr($string,1,strlen($string) -2));
        }
        else
            echo " Not a Palindrome"; }
    }
}
  
$string = "MALAYALAM";
Palindrome($string);
?>  

chevron_right


Output:

Palindrome

Working:
The first character is matched with the last character of the string during each recursive call and if it matches, then both the characters are discarded during the next call. This goes on until the length of the string reduces to 0 or 1. In the following example where the word “MALAYALAM”, is taken, let’s see the working.
In the first step both the M’s at both, the end is compared. Since it matches, both of them are discarded and the next string to be passed is “ALAYALA”. Again both the A matched at both the end, so next string to be passed is “LAYAL”. This goes on until only “Y” remains.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.