+

PHP Palindrome Check

Examples for palindrome number:
Input: 1441 Output: Palindrome Explanation: Reversing 1441 will also get 1441 Input: 12521 Output: Palindrome Explanation remains same
Check the palindrome number Here we just used an iterative way of checking the palindrome number. Each digit is extracted in iteration to form the reciprocal, and finally it is checked if it is the same as the original number.
// PHP code for checking palindrome number in PHP
// Function for checking 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 " ; }  
?>
Output:
Palindrome   Check palindrome string Examples for palindrome string:
Input: "MALAYALAM" Output: Palindrome Explanation: Reversing "MALAYALAM "will also get" MALAYALAM "Input:" 14441 "Output: Palindrome Explanation remains the same
Method 1: Using strrev()
Function strrev() used in PHP for hits lines. We can simply use this method to flip the string and match it to the prevailing value. If this is a match, then the string is a palindrome, otherwise it is not. 
Example: < tbody>
// PHP code for PHP Palindrome string validation
// 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 method using substr()
Method substr() is used to return a portion of a string called a substring. Using this method, there is a recursive way to check the palindrome or not. In this method, no new line is generated and the original line is changed in each recursive call. 
Example: < tbody>
// PHP code for PHP Palindrome string validation
// Recursive way using substr() function Palindrome ( $string ) { // Base encoding to complete recursive process if ((< / code> strlen ( $string ) == 1) || ( strlen ( $strin g ) == 0)) { echo " Palindrome " ; } else {< / code> // The first character is compared to the last if ( substr ( $string , 0,1) == substr ( $string , ( strlen ( $string ) - 1), 1)) { // Verified emails are discarded and sent for next call return Palindrome ( substr ( $string , 1, strlen ( $string ) -2)); } else { echo "Not a Palindrome" ; } } } $string = "MALAYALAM" ; Palindrome ( $string ); ?> Output:
Palindrome  Works:
The first character is matched against the last character of the string during each recursive call, and if it matches, then both characters are discarded during the next call. This continues until the length of the string decreases to 0 or 1. In the following example, where the word "MALAYALAM" is taken, let`s see how it works. 
In the first step, both Ms are in both, the end is compared. Since it is the same, both are discarded and the next line to be passed is & # 8212; "ALAYALA". Again both A`s are the same at both ends, so the next string to be passed is & # 8212; "LAYAL". This continues as long as only "Y" remains.
Get Solution for free from DataCamp guru