Palindrome in PHP

WBOY
Release: 2024-08-29 13:13:29
Original
626 people have browsed it

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 Tests

Start 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.

Palindrome Logic

The logic behind getting a palindrome is as per the following:

  1. Get an input number or string.
  2. Get the reverse of the input number or string using the built-in function.
  3. Compare both the numbers or strings – the input and the reverse number or string.
  4. If the input and the reverse are found to be equal, it means that the number or string is a palindrome.

How to Check Palindrome in PHP?

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.

Example #1

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:

<?php
// example to get the palindrome of a string using built in function
//input string is MADAM
$input = "MADAM";
echo '<br> Input String '. $input;
//reverse of input string - MADAM - using strrev
$reverse = strrev($input);
echo '<br> Ouput String '. $reverse;
//condition to check if the input and the reverse of the string is equal or not
if($input == $reverse) {
echo '<br> '.$input.'  is a palindrome';
}
else {
echo '<br>'.$input.' is not a palindrome';
}
?>
Copy after login

Output:

Palindrome in PHP

Example #2

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:

<?php
//example to get the palindrome of a number using built in function
// input string is 1234321
$input = 1234321;
echo '<br>'.'Input string '. $input;
//reverse of input string using strrev
$reverse = strrev($input);
echo '<br>'.'Reverse string '.$reverse;
//condition to check if the input and the reverse of the string is equal or not
if($input == $reverse) {
echo '<br>'.$input.' is a palindrome';
}
else {
echo '<br>'.$input.' is not a palindrome';
}
?>
Copy after login

Output:

Palindrome in PHP

Example #3

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:

<?php
//example to get the palindrome of a number using built in function
function Palindrome_Function($input) {
// applying strrev() function to input string
$reverse = strrev($input);
//condition to check if reverse and input strings are same or not
if($reverse == $input) {
return true;
}
else
{
return false;
}
}
$input = 1995991;
//calling the reverse function
$result = Palindrome_Function($input);
if($result == TRUE) {
echo $input.' is palindrome';
}
else
{
echo $input.' is not palindrome';
}
?>
Copy after login

Output:

Palindrome in PHP

Example #4

In the below program, we will input a number that is not a palindrome number and see the result.

Code:

<?php
//example to get the palindrome of a number using built in function
function Palindrome_Function($input) {
$reverse = strrev($input);
if($reverse == $input) {
return true;
}
else
{
return false;
}
}
$input = 13241;
$result = Palindrome_Function($input);
if($result == TRUE) {
echo $input.' is palindrome';
}
else
{
echo $input.' is not palindrome';
}
?>
Copy after login

Output:

Palindrome in PHP

Example #5

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:

<html>
<head>
<title>Palindrome Program</title>
</head>
<body>
<form method="post" action="program1.php">
<input type="text" name="number" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['number'])) {
$input = $_POST['number'];
$reverse = strrev($input);
if($reverse == $input) {
echo $input . 'is a palindrome';
}
else{
echo $input. 'is not a palindrome';
}
}
?>
</body>
</html>
Copy after login

Output:

Palindrome in PHP

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:

  1. Get an Input number
  2. Divide the number by 10 to get the remainder
  3. Add the remainder to the new variable, which is multiplied by 10
  4. Divide the number by 10.

Code:

<?php
//example to check if number is palindrome or not without using function only for numbers
//defining the palindrome function
function Palindrome_Function($input) {
$number = $input;
$sum = 0;
//using while loop to get the reverse of the input number
while(floor($number))
{
$remainder = $number % 10;
$sum = $sum * 10 + $remainder;
$number = $number / 10;
}
if($sum == $input) {
return true;
}
else {
return false;
}
}
//passing the input number
$input_number = 1546451;
//calling the Palindrome_Function
$result = Palindrome_Function($input_number);
//check if the input is equal to output of palindrome_function
if($result){
echo "<br>"." $input_number is a Palindrome";   //if equal show $input is palindrome number
} else {
echo "<br>"."$input_number is not a Palindrome";  //if not equal show $input is not a palindrome number
}
?>
Copy after login

Output:

Palindrome in PHP

Conclusion – Palindrome in PHP

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!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!