Palindrome in PHP

WBOY
Release: 2024-08-29 13:13:29
Original
501 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:

 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'; } ?>
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:

'.'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'; } ?>
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:

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:

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:

  Palindrome Program 
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:

"." $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 } ?>
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
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!