Before understanding, Palindrome in PHP we will first study Palindrome. Palindrome means there is no change in the original and the reverse of the string. Meaning that the Palindrome of a number is the same as the original even after the reverse of the string. This can be applied to numbers also.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
For Example:
Input: 12321
Reverse: 12321
Input: Civic
Reverse: Civic
To know whether a string or a number is Palindrome or not, we will use the built-in function in PHP.
The logic behind getting a palindrome is as per the following:
To check the Palindrome of a number, we will use the built-in function called strrev()
About the strrev() function in PHP:This function accepts both string and numbers as the input string. It performs reverse on the input string but does not change the given string. It always returns the reversed form of the given string.
In the following program, we have an input string, MADAM; on this string strrev() function is applied. After applying the function, the result returns the exact string MADAM; then, a condition is checked to determine whether the input and the reversed string are equal or not.
Code:
Input String '. $input; //reverse of input string - MADAM - using strrev $reverse = strrev($input); echo '
Ouput String '. $reverse; //condition to check if the input and the reverse of the string is equal or not if($input == $reverse) { echo '
'.$input.' is a palindrome'; } else { echo '
'.$input.' is not a palindrome'; } ?>
Output:
As seen in the above program, the input string is a palindrome. Let us now apply the same strrev function on a number to check whether the input number is a palindrome or not.
Code:
'.'Input string '. $input; //reverse of input string using strrev $reverse = strrev($input); echo '
'.'Reverse string '.$reverse; //condition to check if the input and the reverse of the string is equal or not if($input == $reverse) { echo '
'.$input.' is a palindrome'; } else { echo '
'.$input.' is not a palindrome'; } ?>
Output:
In the below program, we have used the strrev() built-in function defined within a different function named Palindrome_Function. So when this function is called to reverse a string, it performs reverse on the input string using the strrev() function. You can complete the same program in the following way.
Code:
Output:
In the below program, we will input a number that is not a palindrome number and see the result.
Code:
Output:
Following is the program wherein we have a form containing an input text box. On entering a number and submitting the form, we have the result, which tells us whether the input number is a palindrome or not.
Code:
Output:
In the below program, we have the following steps to get the reverse of a number without using the strrev() function.
We will be using the while loop here:
Code:
"." $input_number is a Palindrome"; //if equal show $input is palindrome number } else { echo "
"."$input_number is not a Palindrome"; //if not equal show $input is not a palindrome number } ?>
Output:
This article explains what a palindrome is, how we find whether a number is a palindrome or not, and how to know whether the input string is a palindrome or not. I hope this article was helpful.
The above is the detailed content of Palindrome in PHP. For more information, please follow other related articles on the PHP Chinese website!