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.
PHP
<?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";
}
?>
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 previous value. If it is a match, then the string is palindrome else not.
Example:
PHP
<?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
// PHP code to check for Palindrome string in PHP
// Recursive way using substr()
function Palindrome($string){
// Base condition 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
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.
Similar Reads
Check if a number is Palindrome in PL/SQL Given an integer, write a function that returns true if the given number is palindrome, else false. For example, 12321 is palindrome, but 1451 is not palindrome. Let the given number be num. A simple method for this problem is to first reverse digits of num, then compare the reverse of num with num.
1 min read
Palindrome in JavaScript We will understand how to check whether a given value is a palindrome or not in JavaScript. A palindrome is a word, phrase, number, or any sequence that reads the same forward and backward. For instance, "madam" and "121" are palindromes.To perform this check we will use the following approaches:Tab
3 min read
Check whether a string is palindrome or not in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a string and the task is to find whether it is Pa
1 min read
Sudo Placement | Palindrome Family Given a string of lowercase characters, the task is to detect the family of string, where family of string is described as follows. ODD Palindrome : String with characters at odd index (1-based indexing) forming Palindrome.EVEN Palindrome : String with characters at even index (1-based indexing) for
9 min read
Check if number is palindrome or not in Octal Given a number which may be in octal or in decimal. If the number is not octal then convert it into octal then check if it is palindrome or not. If it is palindrome then print 1 otherwise print 0. If the number is already in octal then check if the number is palindrome or not.Examples : Input :n = 1
7 min read
How to check the given string is palindrome using JavaScript ? A palindrome is a word, sentence, or even number that reads the same from the back and from the front. Therefore if we take the input, reverse the string and check if the reversed string and the original string are equal, it means the string is a palindrome, otherwise, it is not. Approach: When the
3 min read
Check whether a number is Emirpimes or not Given a number 'n', check whether it is an emirpimes or not. An emirpimes("semiprime" when spelled backwards) derives its definition from the way it is spelt. So, an emirpimes is a number that is a semiprime (product of two prime numbers) itself, and the reversal of its digits gives another new numb
10 min read
Print longest palindrome word in a sentence Given a string str, the task is to print longest palindrome word present in the string str.Examples: Input : Madam Arora teaches Malayalam Output: Malayalam Explanation: The string contains three palindrome words (i.e., Madam, Arora, Malayalam) but the length of Malayalam is greater than the other t
14 min read
To check a number is palindrome or not without using any extra space Given a number 'n' and our goal is to find out it is palindrome or not without using any extra space. We can't make a new copy of the number . Examples: Input : 2332 Output : Yes it is Palindrome. Explanation: original number = 2332 reversed number = 2332 Both are same hence the number is palindrome
6 min read
Check if a number with even number of digits is palindrome or not Given a number N containing an even number of digits. The task is to check whether that number is palindrome or not. Examples: Input: N = 123321 Output: Palindrome Input: 1234 Output: Not palindrome A Naive Approach is to traverse from the front and back of that number and stop where they do not mat
4 min read