Check Palindrome in PHP with Program

This blog explains about Check Palindrome in PHP with Program . They are illustrated very well below :

_______________________________________________________________________________

Hi,

     I give an idea to check a given string is palindrome or not in php. In php, we use the “.” symbol for concatenating two strings. First of all, we should calculate the given string by using strlen(). I am considering the variable $x=”AMMA”; Here the length of the string is 4. We can get the every character from its indexes of  $x[0], $x[1], $[2], $x[3]. Each index values gives A, M, M, A. Then we should concatenate from last index with its previous index values till  its  starting index values by using the for loop.

Ok, Let see program:

<?php
$x=”AMMA”;
$n=””;
$m=strlen($x);
for($i=$m-1; $i>-1; $i–)
{
$n=$n.$x[$i];
}
echo $n.”<br>”;

if($x==$n)
{
echo “Given string is palindrome”;
}
?>

 

OUTPUT:

AMMA
Given string is palindrome

____________________________________________________________________

                                                  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.

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 previous value. If it is a match, then the string is palindrome else not.
Example:

<?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";  
}
?>  

 

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:

<?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);
?>  

Output:

Palindrome
____________________________________________________________________
REFERENCES :

https://www.geeksforgeeks.org/php-palindrome-check/