Open In App

PHP | Palindrome Check

Last Updated : 12 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

How to check given string is Palindrome or not using filter in VueJS ?
Filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an object. A single filter is a function that accepts a value and returns another value. The returned value is the one that’s actually printed in the Vue.js templ
2 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 user clicks on the Submit button, we run a JavaScr
3 min read
JavaScript Program to Check for Palindrome String using Recursion
Given a string, write a recursive function that checks if the given string is a palindrome, else, not a palindrome. A string is called a palindrome if the reverse of the string is the same as the original one. For example - “madam”, “racecar”, etc. What is Recursion?The process in which a function calls itself directly or indirectly is called recur
2 min read
JavaScript Program to Check for Palindrome Number
In this article, we are going to learn about Palindrome Numbers in JavaScript. A palindrome number is a numerical sequence that reads the same forwards and backward, It remains unchanged even when reversed, retaining its original identity. Example: Input : Number = 121Output : PalindromeInput : Number = 1331Output : PalindromeThere are several meth
3 min read
Javascript Program To Check If A Singly Linked List Is Palindrome
Given a singly linked list of characters, write a function that returns true if the given list is a palindrome, else false. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. METHOD 1 (Use a Stack) A simple solution is to use a stack of list nodes. This mainly involves three steps.Traverse the given list from head t
9 min read
How to Check if a String is Palindrome or Not Without Filters in VueJS ?
In VueJS, we can create a simple palindrome checking function by comparing the characters at the proper corresponding positions in the string. We will iterate over half of the string length and check if the characters are the same as each other. In this article, we will check whether the string is palindrome or not by using the two different approa
2 min read
Check Whether a Year is a Palindrome Year using JavaScript
A palindrome year is a year that remains the same when its digits are reversed. For example, 2002 is a palindrome year because it reads the same backward checks whether as forward. In this problem, we're tasked with checking whether a given year is a palindrome year. Table of Content Using string manipulationUsing Arithmetic OperationsUsing string
2 min read
JavaScript Program to Check if an Array is Palindrome or Not
A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. To check if an array is a palindrome, compare it to its reverse version. If they match, it's a palindrome. Given an array, the task is to determine whether an array is a palindrome or not. Examples: Input: arr = [3, 6, 0, 6, 3]Output: P
3 min read
How to check whether a passed string is palindrome or not in JavaScript ?
In this article, we are given a string, our task is to find string is palindrome or not. A palindrome is a series of numbers, strings, or letters that, when read from right to left and left to right, match each other exactly or produce the same series of characters. in simple words when number strings or characters are inverted and still provide th
4 min read
JavaScript Program to Check if a Given String is a Rotation of a Palindrome
In this article, we will see how to check a given string is a rotation of a palindrome. A palindrome is a string that remains the same when its characters are reversed (e.g., "racecar" is a palindrome). Example: Input: str = "racecar"Output: true // "racecar" is a rotation of a palindrome "racecar"Input: str = "abbab"Output: true// "abbab" is a rot
4 min read