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, $x, $, $x. 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:
for($i=$m-1; $i>-1; $i–)
echo “Given string is palindrome”;
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.
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.