Check Palindrome in PHP with Program

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

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.

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()

functionPalindrome($string){

if(strrev($string) == $string){

return1;

}

else{

return0;

}

}

// Driver Code

$original= "DAD";

if(Palindrome($original)){

echo"Palindrome";

}

else{

echo"Not a Palindrome";

}

?>

Output:

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: